java web 多图片打包下载,弹出提示框问题_html/css_WEB-ITnose

php中文网
发布: 2016-06-24 11:44:30
原创
1368人浏览过

在spring mvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?

Controller方法

@RequestMapping("/tcdl")
public ModelAndView dlCode(HttpServletRequest request,HttpServletResponse response,@RequestParam(value="ids") String ids,@RequestParam(value = "funId", required = false) Integer funId) throws IOException{
ModelAndView mav = new ModelAndView();
//response.setHeader("Charset", "UTF-8");
//response.setContentType("text/html; charset=utf-8");
List fileList = new ArrayList();
String path = request.getSession().getServletContext().getRealPath("");
String[] trims = ids.split(",");
String type="";  //文件格式后缀
for(int i=0;i Operator oper = opService.getOperatorById(Integer.parseInt(trims[i]));
if(!"".equals(oper.getOpCardUrl())){
File f = new File(path+oper.getOpCodeUrl());
/*int k = oper.getOpCodeUrl().indexOf("other");   ///userPic/3/other/2014011617535382684910.png
int j =0;
while (j != -1) {
j = oper.getOpCodeUrl().indexOf(".");
type = type.substring(j + 1);
}
File f = new File(path+oper.getOpCodeUrl());
String newName = oper.getOpCodeUrl().substring(0,k+1)+oper.getOpUserName()+oper.getOpMobile()+type;
System.out.println(newName+",newName");*/
if(f.exists()){
// f.renameTo(new File(path+oper.getOpCodeUrl().substring(0,k+1)+newName));
fileList.add(f);
}
}
}
String fileName = "twoCodeDown"+".zip";
/**在服务器端创建打包下载的临时文件*/
 File f = new File(path+"/tmp");
 if(!f.exists()){
 f.mkdirs();
 }
 String outFilePath = path+"/tmp/"+fileName;
 File file = new File(outFilePath);
 /**文件输出流*/
 FileOutputStream outPutStream = new FileOutputStream(file);
 /**压缩流*/
 ZipOutputStream toClient = new ZipOutputStream(outPutStream);
 //this.downloadZip(file);
 /**压缩文件*/
 opService.downloadZip(fileList,toClient);
 /**下载压缩*/
 opService.downloadZip(file, response);
 return mav;
}
下载方法
**
 * 下载打包的文件
 * @throws IOException 
 * */
public void downloadZip(File file,HttpServletResponse response) throws IOException{
/**依流的形式下载文件*/
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file.getPath()));
FileOutputStream outStream = new FileOutputStream(file.getPath());
byte[] buffer = new byte[bis.available()];
bis.read(buffer);
bis.close();
OutputStream toClient = new BufferedOutputStream(outStream);
response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
toClient.write(buffer);
toClient.flush();
toClient.close();
file.delete();      //将生成的服务器端文件删除
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}


回复讨论(解决方案)

你用的是ajax来请求的,ajax请求是不会弹出提示保存位置选框的。建议你用js或jquery动态的创建form表单来提交,记得要加上
response.setHeader("Content-Disposition", "attachment;filename="+imageName);。

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号