实现文件批量选择,压缩为zip下载. 遇到的问题:ajax提交无法下载文件
2018-02-08 16:38
686 查看
要做一个批量选择文件,压缩成zip下载的功能,一直无法下载
最终的问题是ajax提交时无法正常下载文件,但是可以再success中访问 @RequestMapping("/batchDown/{mongoids}")
public void batchDown(@PathVariable(value="mongoids",required=false) String mongoids,
HttpServletResponse response, HttpServletRequest request) {
String zipName =getZipFilename();
// 头文件
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
ZipOutputStream zos = null;
// 压缩文件
try {
zos = new ZipOutputStream(response.getOutputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
// --拼装
ArrayList<File> fileList = new ArrayList<File>();
String[] mongoidArr = mongoids.split(",");
for (String mongoid : mongoidArr) {
GridFSDBFile gfs = FileUtil.getFile(mongoid, "oa", mongoTemplate);
String filename = oaFileService.getFileNameByMongoId(mongoid);
File f = new File(filename);
try {
ZipUtils.inputstreamtofile(gfs.getInputStream(), f);
fileList.add(f);
} catch (IOException e) {
e.printStackTrace();
}
}
File[] files = fileList.toArray(new File[fileList.size()]);
try {
zipFile(files, "", zos);
zos.flush();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 打包文件
*
* @param subs
* 文件数组
* @param baseName
* 自定义名字
* @param zos
* 输出流
* @throws IOException
*/
private void zipFile(File[] subs, String baseName, ZipOutputStream zos) throws IOException {
for (int i = 0; i < subs.length; i++) {
File f = subs[i];
zos.putNextEntry(new ZipEntry(baseName + f.getName()));
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
}
/**
* 压缩包名字
*
* @return
*/
private String getZipFilename() {
Date date = new Date();
String s = date.getTime() + ".zip";
return s;
}
前台最终采用链接打方法进行访问
location.href="/oa/OAFile/batchDown/"+mongoids
最终的问题是ajax提交时无法正常下载文件,但是可以再success中访问 @RequestMapping("/batchDown/{mongoids}")
public void batchDown(@PathVariable(value="mongoids",required=false) String mongoids,
HttpServletResponse response, HttpServletRequest request) {
String zipName =getZipFilename();
// 头文件
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + zipName);
ZipOutputStream zos = null;
// 压缩文件
try {
zos = new ZipOutputStream(response.getOutputStream());
} catch (IOException e1) {
e1.printStackTrace();
}
// --拼装
ArrayList<File> fileList = new ArrayList<File>();
String[] mongoidArr = mongoids.split(",");
for (String mongoid : mongoidArr) {
GridFSDBFile gfs = FileUtil.getFile(mongoid, "oa", mongoTemplate);
String filename = oaFileService.getFileNameByMongoId(mongoid);
File f = new File(filename);
try {
ZipUtils.inputstreamtofile(gfs.getInputStream(), f);
fileList.add(f);
} catch (IOException e) {
e.printStackTrace();
}
}
File[] files = fileList.toArray(new File[fileList.size()]);
try {
zipFile(files, "", zos);
zos.flush();
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 打包文件
*
* @param subs
* 文件数组
* @param baseName
* 自定义名字
* @param zos
* 输出流
* @throws IOException
*/
private void zipFile(File[] subs, String baseName, ZipOutputStream zos) throws IOException {
for (int i = 0; i < subs.length; i++) {
File f = subs[i];
zos.putNextEntry(new ZipEntry(baseName + f.getName()));
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
}
/**
* 压缩包名字
*
* @return
*/
private String getZipFilename() {
Date date = new Date();
String s = date.getTime() + ".zip";
return s;
}
前台最终采用链接打方法进行访问
location.href="/oa/OAFile/batchDown/"+mongoids
相关文章推荐
- strutsmvc ajaxForm提交表单ie出现json文件下载问题解决
- input file 在开发中遇到的问题 类似ajax form表单提交 input file中的文件
- 使用struts2实现下载功能遇到的文件中文问题解决经验分享
- 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能 (转载并优化处理篇)
- strtus2 批量下载 中文问题、压缩文件等 ------ 讨论struts2工作流程
- Servlet实现文件下载以及遇到的问题
- java实现文件压缩下载----压缩下载zip
- Java实现zip压缩多个文件下载
- 关于Ajax无法下载文件到浏览器本地的问题
- strtus2 批量下载 中文问题、压缩文件等 ------ 讨论struts2工作流程 (摘自:http://www.blogjava.net/xcp/archive/2009/10/30/downloadlist.html)
- Android实现下载zip压缩文件并解压的方法(附源码)
- 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能
- 【Java】Java实现zip压缩多个文件下载
- ajax调用后台实现文件下载,不弹出下载提示框,无法下载
- 一个IE7下,用form表单跳转iframe提交,然后下载文件。遇到的很有意思的问题
- Java操作Zip压缩文件遇到的编码问题
- java web 实现把文件夹下的所有文件压缩成zip并实现下载(原文链接)http://blog.csdn.net/sxdtzhaoxinguo/article/details/20550537)
- strtus2 批量下载 中文问题、压缩文件等 ------ 讨论struts2工作流程
- Jsp实现筛选并压缩文件批量下载
- Java实现zip压缩多个文件下载