代码没有报错,但是点击下载后,页面上没有弹出保存文件的对话框,使用火狐firebug调试,在返回的数据页面右键“在新的标签页中打开”,才会出来
//批量下载高铁作业室的图片,AJAX方式
@RequestMapping(params="method=downloadBoxImages")
public void downloadBoxImages(int id, HttpServletRequest req,HttpServletResponse resp) throws IOException{
List<BoxImages> bi = this.boxInfoService.getBoxImages(id);
resp.setContentType("application/x-msdownload");
resp.setHeader("Content-Disposition", "attachment;filename=pictures.zip");
String path = getServletcontext().getRealPath("/");
String rootpath = path.substring(0, (path.length()-4)) + "gjx_uploadfile\\";
int len = bi.size();
String[] filenames = new String[len];
for (int i = 0; i < len; i ++)
{
filenames[i] = bi.get(i).getImage_path();
}
String str = "";
String rt = "\r\n";
ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
for(String filename : filenames){
str += filename + rt;
File file = new File(rootpath + filename);
zos.putNextEntry(new ZipEntry(filename));
FileInputStream fis = new FileInputStream(file);
byte b[] = new byte[1024];
int n = 0;
while((n = fis.read(b)) != -1){
zos.write(b, 0, n);
}
zos.flush();
fis.close();
}
zos.setComment("download success:" + rt + str);
zos.flush();
zos.close();
}