one123

导航

将zip文件以流的形式输出到页面

//将文件以流的形式输出
//获取到zip文件的地址
String zipPath = businessMessageInter.downlodImgs(bussNo,imageIndexList,userLoginOut);
HttpServletResponse response = SessionUtil.getResponse();
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
// 2.设置文件头:最后一个参数是设置下载文件名
response.setHeader("Content-Disposition", "attachment;fileName=" + zipPath+".zip");
File zipFile = new File(zipPath+".zip");
FileInputStream fis = null;
ServletOutputStream out = null;
try {
//获取输入流
fis = new FileInputStream(zipFile);
//获取输出流
out = response.getOutputStream();
int b = 0;
byte[] buffer = new byte[1024];
//遍历输入流到输出流
while ((b = fis.read(buffer)) != -1) {
// 4.写到输出流(out)中
out.write(buffer, 0, b);
}
fis.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}

posted on 2017-06-21 15:32  Fainer  阅读(922)  评论(0编辑  收藏  举报