您的位置:首页 > 编程语言 > Java开发

Struts2下载zip压缩文件-不生成临时文件

2013-12-26 16:21 363 查看
<!--Struts2配置文件-->
<action name="statementDownloadZipForExcel"
class="自己的包.action.StatementsDownloadAction"
method="downloadZipForExcel">
</action>

对应的action类中的方法

public String downloadZip()throws Exception {
String path = "具体的文件存放目录";
List<String> filenames= new ArrayList<String>();//具体的文件名称
        File file = null;
byte[] bufb = null;
ZipEntry ze = null;
BufferedInputStream bis = null;
HttpServletResponse response = ServletActionContext.getResponse();
//清空输出流
response.reset();

//设定输出文件头
response.setHeader("Content-Disposition","attachment;filename="+fileName);
response.setContentType("application/zip");
zos = new ZipOutputStream(response.getOutputStream());
for(String csvname:filenames){
file = new File(path+csvname);
bufb = new byte[1024];
ze = new ZipEntry(csvname);
zos.putNextEntry(ze);
bis = new BufferedInputStream(new FileInputStream(file));
int len;
while ((len = bis.read(bufb)) > 0) {
zos.write(bufb, 0, len);
}
}
zos.closeEntry();
bis.close();
zos.flush();
zos.close();
return null;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2 压缩 zipdownload