<!--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框架实现的批量下载ZIP文件的功能。通过配置Struts2的action,可以将多个CSV文件打包成一个ZIP文件供用户下载。示例代码展示了如何读取文件并创建ZIP输出流。
1667

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



