SpringMVC文件打包下载
来源:互联网 发布:apache开启rewrite 编辑:程序博客网 时间:2024/05/29 21:36
最近做了一个ABTest的应用,用户希望对ABTest的结果能够打包批量进行下载,这个时候就需要先对下载的多个文件进行压缩打包,再进行下载。
@RequestMapping("/download")public Object export(HttpServletRequest request, HttpServletResponse response, @RequestParam String batchId,) throws IOException { List<UserInfo> list = userDao.getUsers(batchId); String tmpdir = System.getProperty("java.io.tmpdir"); if(StringUtils.isEmpty(tmpdir)){ logger.info("tmpdir is empty, use ServletContextPath"); tmpdir = request.getServletContext().getContextPath(); } logger.info("export tmpdir:{}", tmpdir); File tempDir = new File(tmpdir+File.separator+"download"); if(!tempDir.exists()){ boolean success = tempDir.mkdirs(); logger.info("create tempDir:{}, success:{}", tempDir.getAbsolutePath(), success); } File zipFile = new File(tempDir, String.format("%s.zip", batchId)); //压缩后的文件 OutputStream out = null; InputStream in = null; try { convert(list, zipFile); response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName()); out = response.getOutputStream(); in = new FileInputStream(zipFile); IoUtils.copy(in, out); } catch (IOException e) { logger.error("export file error", e); }finally { IoUtils.closeQuietly(out); IoUtils.closeQuietly(in); }}
private void convert(List<UserInfo> list, File zipFile){ ZipOutputStream zipOut = null; try { zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); for (UserInfo info : list){ addZipEntry(zipOut, info.getId()+".txt", info.getName()+"\t"+info.getPassword()); } zipOut.flush(); } catch (Exception e) { logger.error("export data failed, type:"+type+", batchId:"+batchId, e); throw new RuntimeException("export data failed, type:"+type+", batchId:"+batchId, e); }finally { IoUtils.closeQuietly(zipOut); }}
private void addZipEntry(ZipOutputStream zipOut, String name, String data) throws IOException { zipOut.putNextEntry(new ZipEntry(name)); zipOut.setComment(""); if(StringUtils.isEmpty(data)){ data = " "; } zipOut.write(data.getBytes("UTF-8"));}
0 0
- SpringMVC文件打包下载
- SpringMVC下打包文件的下载
- springmvc批量文件打包成zip下载功能
- springmvc批量文件打包成zip下载功能
- springmvc批量文件打包成zip下载功能
- springmvc批量将文件打包成zip下载
- 文件批量打包下载 框架springmvc
- java文件打包下载
- 文件打包下载
- C#文件打包下载
- 批量打包 下载文件
- 文件打包下载
- java 打包下载文件
- java打包下载文件
- java文件打包下载
- php文件打包下载
- php打包下载文件
- 文件打包下载
- FPGA
- dubbox开发rest+json指南
- 知识库
- Android开发之多线程编程Thread和Runnable使用
- C++的流输入输出与C的scanf,printf的区别在哪里?
- SpringMVC文件打包下载
- WPF 启动程序弹出函数求值已禁用,因为内存出现内存不足异常
- PDF转换为HTML5的四种方式
- git基础:5.远程仓库
- java合并string[]数组
- MySQL导入导出
- javaBean的属性命名规范
- 微信小程序例子——checkbox的使用
- Servlet的ServletConfig对象获取初始化参数