HTML 标签
'<a href="/utils/fileStream/exportKeHuFaPiaoZhangDan?fileId=666">'
JAVA Controller
@RequestMapping(value = "/export")
@ResponseBody
public void export(HttpServletRequest request, HttpServletResponse response, String fileId) {
try {
InputStream is = getInputStream(kehuApiConfig.getUrl() , fileId);
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = is.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in_b = swapStream.toByteArray();
is.close();
swapStream.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename="+ fileId +".xlsx" );
OutputStream os = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
os.write(in_b);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static InputStream getInputStream(String fileUrl, String fileId) {
JSONObject paramJson = new JSONObject();
paramJson.put("fileId", fileId);
return sendPostByRawRetInputStream(fileUrl, paramJson.toJSONString(), ContentType.APPLICATION_JSON);
}
public static InputStream sendPostByRawRetInputStream(String url, String param, ContentType contentType) {
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
InputStream inputStream = null;
try {
httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
httpPost = new HttpPost(url);
httpPost.setConfig(requestConfig);
httpPost.setEntity(new StringEntity(param, contentType));
CloseableHttpResponse response = httpClient.execute(httpPost);
HttpEntity temp = null;
HttpEntity entity = response.getEntity();
temp = entity;
inputStream = temp.getContent();
} catch (Exception e) {
throw new BusinessException(e.getMessage());
}
return inputStream;
}