RainbowPeter
2015-08-09 15:51
//批量下载高铁作业室的图片,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();
}
哈哈,代码中关闭默认打开方式就可以了
前台jsp页面配action了吗?
放了半天没人解决,自己解决了,给自己个好评吧
Java中的文件上传下载
77393 学习 · 337 问题
相似问题