java接口转发上传的文件
@RequestMapping(value = "/XXXX/fileUpload", method = RequestMethod.POST)
public String getFileUpload2(@RequestParam("file") MultipartFile file, HttpServletRequest request)
public static String hotMapPost3(String url, String jsonParams,String dddd, MultipartFile file, HttpServletRequest request) {
JSONObject result = null;
try {
MultiValueMap params = new LinkedMultiValueMap<>();
ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
@Override
public String getFilename() {
return file.getOriginalFilename();
}
};
params.add("file",resource);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "multipart/form-data");
httpHeaders.add("userLoginToken", dddd);
String orgCode = request.getHeader("orgCode");
String orgName = request.getHeader("orgName");
httpHeaders.add("orgCode", orgCode);
httpHeaders.add("orgName", orgName);
HttpEntity> stringHttpEntity2 = new HttpEntity<>(params, httpHeaders);
result = restTemplate.postForObject(url , stringHttpEntity2, JSONObject.class);
} catch (Exception e) {
log.info(e.toString());
}
if(null == result){
return null;
}else {
return result.toJSONString();
}
}
java接口转发下载的文件
public static void postDownLoadStream(String uri, String jsonPara, HttpServletResponse response, Mapheader) { HttpURLConnection connection = null; log.info("下载类型接口调用开始: uri = {}", uri); String result = ""; long currentTimeMillisStart = System.currentTimeMillis(); try { URL urlNet = new URL(uri); connection = (HttpURLConnection) urlNet.openConnection(); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setConnectTimeout(20000); connection.setReadTimeout(20000); connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); if (header != null) { Iterator > iterator = header.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry next = iterator.next(); connection.setRequestProperty(next.getKey(), next.getValue()); } } connection.connect(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8")); writer.write(jsonPara); writer.close(); int responseCode = connection.getResponseCode(); log.info("httpResponseCode = {}", responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { String contentType = connection.getContentType(); String content = connection.getHeaderField("Content-Disposition"); response.reset(); response.setContentType(contentType); response.setHeader("Content-Disposition", content); OutputStream outputStream = response.getOutputStream(); BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream()); byte[] temp = new byte[1024]; int lenth; while ((lenth = inputStream.read(temp)) > 0) { outputStream.write(temp, 0, lenth); } outputStream.flush(); outputStream.close(); } else { log.warn("http failed , httpResponseCode = {}", responseCode); throw new RuntimeException("HttpInvoker httpResponseCode " + uri + " " + responseCode); } } catch (MalformedURLException e) { log.warn("http failed , MalformedURLException ", e); throw new RuntimeException("HttpInvoker MalformedURLException " + uri, e); } catch (IOException e) { log.warn("http failed , IOException ", e); throw new RuntimeException("HttpInvoker IOException " + uri, e); } catch (Throwable e) { log.warn("http failed , Throwable ", e); throw new RuntimeException("HttpInvoker Throwable " + uri, e); } finally { if (connection != null) { connection.disconnect(); } long currentTimeMillisEnd = System.currentTimeMillis(); log.info("HttpInvoker post url = {} time-consuming = {}", uri, currentTimeMillisEnd - currentTimeMillisStart); log.info("HttpInvoker end post url = {} response = {}", uri, result); } }