Springboot接收文件流转发到其他服务器
1.controller接收前端传过来的文件流
@Autowired
private UploadService uploadService;
@RequestMapping("/upload")
public int uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
InputStream inputStream = file.getInputStream();
return uploadService.uploadFile("https://10.254.254.185:9090/login_xml.php", file.getOriginalFilename(), inputStream );
}
2.UploadServiceImpl中接收输入流转发到指定URL地址的服务器中
public int uploadFile(String urlStr, String filename, InputStream fileStream) {
int ret = -1;
String BOUNDARY = "---------------------------41184676334";
int blockSize = 1024;
HttpURLConnection conn = null;
try {
URL url = new URL(urlStr);
if (url.getProtocol().toLowerCase().equals("https")) {
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
//去掉https认证
TrustManager[] managers = {
new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
}
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, managers, new SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
});
conn = httpsConn;
} else {
conn = (HttpURLConnection) url.openConnection();
}
conn.setConnectTimeout(20000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/html");
conn.setRequestProperty("Cache-Control", "no-cache");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
conn.setAllowUserInteraction(false); //无需用户交互,即弹出https等的对话框。
conn.setChunkedStreamingMode(blockSize);
OutputStream out = conn.getOutputStream();
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
StringBuffer sb = new StringBuffer();
sb.append("--").append(BOUNDARY).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"name\"\r\n\r\n");
sb.append(filename);
sb.append("\r\n--").append(BOUNDARY).append("\r\n");
sb.append("--").append(BOUNDARY).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n");
sb.append("Content-Type: application/octet-stream\r\n\r\n");
byte[] data = sb.toString().getBytes();
out.write(data); //发送非文件数据
int bytes = 0;
byte[] bufferOut = new byte[blockSize];
while ((bytes = fileStream.read(bufferOut, 0, blockSize)) != -1) {
out.write(bufferOut, 0, bytes);
}
out.write(end_data);
out.flush();
out.close();
ret = 0;
} catch (Exception e) {
e.printStackTrace();
}
if (conn != null) {
conn.disconnect();
conn = null;
}
return ret;
}
参考JAVA使用HttpUrlConnection实现自动上传文件
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。