用户访问页面(服务A) 下载 通过接口 现在 服务B 的文件 并下载到本地
@RequestMapping(value = "/downLoad", method = RequestMethod.GET)
public void downLoad(Long id, HttpServletResponse response,String plyNo) {
InputStream is = null;
response.addHeader("pragma","NO-cache");
response.addHeader("Cache-Control","no-cache");
response.addDateHeader("Expries",0);
response.setContentType("application/pdf;charset=utf-8");
String filename = plyNo+".pdf";//文件名称
try {filename = new String(filename.getBytes("UTF-8"), "ISO8859_1");}catch (Exception e) {e.printStackTrace();}
response.addHeader("Content-Disposition","attachment;filename=" + filename);
OutputStream out = null;
try{
//远程服务器接口地址
String toURL = SysConfigUtil.getProperties_1("HT_PLYPDF_DOWN");
//请求接口 并获取文件流
URL url = new URL(toURL+"?policyNo="+plyNo);
URLConnection conn = url.openConnection();
is = conn.getInputStream();
out = response.getOutputStream();
int length = 0;
byte buffer[] = new byte[1024];
while((length = is.read(buffer)) != -1){
out.write(buffer, 0, length);
}
} catch (Exception e) {
} finally{
//close....
}
}
本文介绍了一个从一个服务请求另一个服务的文件并通过HTTP响应将其下载到客户端的Java实现。该过程涉及设置HTTP头防止缓存,定义正确的MIME类型,处理文件名编码,并使用流将远程文件内容传输到客户端。
6163

被折叠的 条评论
为什么被折叠?



