代码没有报错,但是点击下载后,页面上没有弹出保存文件的对话框,使用火狐firebug调试,在返回的数据页面右键“在新的标签页中打开”,才会出来

来源:3-5 使用SmartUpload实现批量下载

RainbowPeter

2015-08-09 15:51

55c706420001ede305000220.jpg

55c70643000119f705000254.jpg

//批量下载高铁作业室的图片,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();

}


写回答 关注

3回答

  • qq_慕沐8227230
    2019-01-02 16:28:11

    哈哈,代码中关闭默认打开方式就可以了

  • 橘子甜心
    2016-10-10 15:08:44

    前台jsp页面配action了吗?

  • RainbowPeter
    2015-08-09 19:45:40

    放了半天没人解决,自己解决了,给自己个好评吧

Java中的文件上传下载

由简入深,轻松实现文件上传下载功能及相关框架的使用

77391 学习 · 337 问题

查看课程

相似问题