java调⽤接⼝实现⽂件上传import com.base.IOpenApi;
import com.base.OpenAuthConfig;
slf4j.Slf4j;
import llections.map.HashedMap;
import t.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.annotation.Resource;
java创建文件
import java.io.*;
import java.HttpURLConnection;
import java.URL;
import java.util.Map;
import java.util.UUID;
/**
* @author huqi 2021/8/12
*/
@Slf4j
@Configuration
public class UploadFile {
@Resource
private OpenAuthConfig openAuthConfig;
public File transferToFile(MultipartFile multipartFile) {
/
/ 选择⽤缓冲区来实现这个转换即使⽤java 创建的临时⽂件使⽤ ansferto()⽅法。
File file = null;
try {
String filename = Resource().getFilename();
String suffix = filename.substring(filename.lastIndexOf("."));
ateTempFile(UUID.randomUUID().toString(), suffix);
file.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
public String upLoadFilePost(String actionUrl, MultipartHttpServletRequest request, String token) throws IOException {        Map<String, MultipartFile> fileTemp = FileMap();
Map<String, File> files = new HashedMap();
for(String key : fileTemp.keySet()){
files.put(key,(key)));
}
actionUrl = DomainName() + actionUrl;
log.info("post地址"+actionUrl);
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY);
conn.setRequestProperty(IOpenApi.BASIC_HEADER_TOKEN, IOpenApi.TOKEN_BEARER + token);
DataOutputStream outStream = new DataOutputStream(
// 发送⽂件数据
if (files != null) {
for (Map.Entry<String, File> file : Set()) {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""
+ Value().getName() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.String().getBytes());
InputStream is = new Value());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.Bytes());
}
}
// 请求结束标志
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();        outStream.write(end_data);
outStream.flush();
// 得到响应码
int res = ResponseCode();
if (res == 200) {
InputStream in = InputStream();
InputStreamReader isReader = new InputStreamReader(in);
BufferedReader bufReader = new BufferedReader(isReader);
String line = "";
String data = "";
while ((line = adLine()) != null) {
data += line;
}
outStream.close();
conn.disconnect();
return data;
}
outStream.close();
conn.disconnect();
return null;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。