Java实现zip压缩多个文件下载
来源:互联网 发布:周易取名软件破解版 编辑:程序博客网 时间:2024/06/03 21:39
转载http://www.cnblogs.com/yzuzhang/p/4763606.html
package com.javaweb.entity;import java.io.Serializable;/** * 文件实体类*/public class FileBean implements Serializable{ private static final long serialVersionUID = -5452801884470115159L; private Integer fileId;//主键 private String filePath;//文件保存路径 private String fileName;//文件保存名称 public FileBean(){ } //Setters and Getters ...}
接下来,在控制层的方法里(示例为Spring MVC),进行读入多个文件List<FileBean>,压缩成myfile.zip输出到浏览器的客户端:
/** * 打包压缩下载文件 */ @RequestMapping(value = "/downLoadZipFile") public void downLoadZipFile(HttpServletResponse response) throws IOException{ String zipName = "myfile.zip"; List<FileBean> fileList = fileService.getFileList();//查询数据库中记录 response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition","attachment; filename="+zipName); ZipOutputStream out = new ZipOutputStream(response.getOutputStream()); try { for(Iterator<FileBean> it = fileList.iterator();it.hasNext();){ FileBean file = it.next(); ZipUtils.doCompress(file.getFilePath()+file.getFileName(), out); response.flushBuffer(); } } catch (Exception e) { e.printStackTrace(); }finally{ out.close(); } }
最后,附上ZipUtils压缩文件的工具类,这样便实现了多文件的压缩下载功能:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipUtils { private ZipUtils(){ } public static void doCompress(String srcFile, String zipFile) throws IOException { doCompress(new File(srcFile), new File(zipFile)); } /** * 文件压缩 * @param srcFile 目录或者单个文件 * @param zipFile 压缩后的ZIP文件 */ public static void doCompress(File srcFile, File zipFile) throws IOException { ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(zipFile)); doCompress(srcFile, out); } catch (Exception e) { throw e; } finally { out.close();//记得关闭资源 } } public static void doCompress(String filelName, ZipOutputStream out) throws IOException{ doCompress(new File(filelName), out); } public static void doCompress(File file, ZipOutputStream out) throws IOException{ doCompress(file, out, ""); } public static void doCompress(File inFile, ZipOutputStream out, String dir) throws IOException { if ( inFile.isDirectory() ) { File[] files = inFile.listFiles(); if (files!=null && files.length>0) { for (File file : files) { String name = inFile.getName(); if (!"".equals(dir)) { name = dir + "/" + name; } ZipUtils.doCompress(file, out, name); } } } else { ZipUtils.doZip(inFile, out, dir); } } public static void doZip(File inFile, ZipOutputStream out, String dir) throws IOException { String entryName = null; if (!"".equals(dir)) { entryName = dir + "/" + inFile.getName(); } else { entryName = inFile.getName(); } ZipEntry entry = new ZipEntry(entryName); out.putNextEntry(entry); int len = 0 ; byte[] buffer = new byte[1024]; FileInputStream fis = new FileInputStream(inFile); while ((len = fis.read(buffer)) > 0) { out.write(buffer, 0, len); out.flush(); } out.closeEntry(); fis.close(); } public static void main(String[] args) throws IOException { doCompress("D:/java/", "D:/java.zip"); } }
其他:spring mvc 下载普通单个文件的方法:
@RequestMapping(value = "/downloadFile") @ResponseBody public void downloadFile (HttpServletResponse response) { OutputStream os = null; try { os = response.getOutputStream(); File file = new File("D:/javaweb/demo.txt"); // Spring工具获取项目resources里的文件 File file2 = ResourceUtils.getFile("classpath:shell/init.sh"); if(!file.exists()){ return; } response.reset(); response.setHeader("Content-Disposition", "attachment;filename=demo.txt"); response.setContentType("application/octet-stream; charset=utf-8"); os.write(FileUtils.readFileToByteArray(file)); } catch (Exception e) { e.printStackTrace(); }finally{ IOUtils.closeQuietly(os); } }
补充,另外一种 利用 ResponseEntity<byte[]> 实现下载单个文件的方法:
/** * Spring下载文件 * @param request * @throws IOException */ @RequestMapping(value="/download") public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException{ // 获取项目webapp目录路径下的文件 String path = request.getSession().getServletContext().getRealPath("/"); File file = new File(path+"/soft/javaweb.txt"); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentDispositionFormData("attachment", "javaweb.txt"); return new ResponseEntity<byte[]>(org.apache.commons.io.FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED); } <a target="_blank" href="/download">点击下载</a>
阅读全文
0 0
- 【Java】Java实现zip压缩多个文件下载
- Java实现zip压缩多个文件下载
- JAVA实现对多个文件压缩为zip格式的下载
- java实现单个文件多个文件的zip压缩
- java实现文件压缩下载----压缩下载zip
- 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能
- java zip压缩多个文件
- 使用PHP的ZipArchive类实现多个文件的zip压缩包打包下载
- java实现文件zip压缩
- java实现压缩zip文件
- java导出多个excel压缩zip下载
- java实现word下载及打包成zip下载(单个文件、多个文件)
- java web 实现多个文件压缩下载
- java下载压缩包文件zip
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
- zip压缩、文件下载
- JAVA实现多文件以ZIP压缩包导出
- java实现zip文件压缩,解压
- CGI详解(原理,配置及访问)
- 程序员从普通到优秀的素质锻炼
- MongoDB (四):索引
- JAVA MVC
- 利用Eclipse编写minigui的helloworld程序
- Java实现zip压缩多个文件下载
- Mysql添加索引
- springboot定时器
- 美国房产投资前景 业内人士看好二级城市多户住宅投资
- spring boot 整合log4j
- python2 去除 字符串中emoji 符号,去除所有4字节utf8字符
- 卷积神经网络(深度学习)一些问题总结
- 算法之最大子数组
- 利用Eclipse编写minigui的helloworld程序