1、代码
private OutputStream res;
private ZipOutputStream zos;
public OutputStream getRes() {
return res;
}
public void setRes(OutputStream res) {
this.res = res;
}
public ZipOutputStream getZos() {
return zos;
}
public void setZos(ZipOutputStream zos) {
this.zos = zos;
}
public String downLoadFile() throws IOException
{
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
id = request.getParameter("id");
if(id==null || id.length()==0)
{
msg = new BaseActionMsg("0","参数不正确");
}
else
{
res = response.getOutputStream();
response.reset();
String zipFileName =id+".zip";
response.setHeader("Content-Disposition",
"attachment;filename="+zipFileName);
response.setContentType("application/zip");
zos = new ZipOutputStream(res);
String activeChangePath = common.PropertiesOper.get("historyPath");
String histroyFilePath=activeChangePath+id;
File file = new File(histroyFilePath);
File[] files = file.listFiles();
if(files==null)
{
msg = new BaseActionMsg("0","找不到相关文件");
}
else
{
for(File f:files)
{
InputStream is = new FileInputStream(f);
byte[] buf = new byte[8192];
int len;
ZipEntry ze = new ZipEntry(f.getName());
zos.putNextEntry(ze);
BufferedInputStream bis = null;
if (is != null) {
bis = new BufferedInputStream(is);
while ((len = bis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
}
is.close();
bis.close();
zos.closeEntry();
}
msg = new BaseActionMsg("1","下载完成");
zos.close();
res.close();
}
}
return "success";
}
2、xml配置
<struts>
<package name="ICBC" namespace="/ICBC" extends="struts-default,json-default">
<action name="downLoadFile" method="downLoadFile" class="IcbcAction">
<result type="json" name="success">
<param name="root">msg</param>
</result>
<result type="json" name="error">
<param name="root">msg</param>
</result>
</action>
</package>
</struts>