springMVC实现基本文件夹压缩下载功能

将文件夹压缩后下载:

 1 @Slf4j
 2 public class Test {
 3 
 4     private static final String BASE_PATH = "/root/doc/";
 5 
 6     private static final String TEMP_PATH = "/root/temp/";
 7     
 8     private static final String FILE_NAME = "测试";
 9     
10     @RequestMapping(value = "download", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE})
11     public ResponseEntity<byte[]> download() throws Exception {
12         fileToZip(BASE_PATH, TEMP_PATH, FILE_NAME);
13         File file = new File(TEMP_PATH + "//" + FILE_NAME + ".zip");
14         HttpHeaders headers = new HttpHeaders();
15         // 为了解决中文名称乱码问题
16         String fileName = new String(FILE_NAME.getBytes("UTF-8"), "iso-8859-1");
17         headers.setContentDispositionFormData("attachment", fileName + ".zip");
18         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
19         return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED);
20     }
21 
22     private static void fileToZip(String sourceFilePath, String zipFilePath, String fileName) throws Exception {
23         File sourceFile = new File(sourceFilePath);
24         if (!sourceFile.exists()) {
25             log.info("{} >>>> is not exists", sourceFilePath);
26             throw new IotBusinessException(ExceptionDefinition.FILE_PATH_NOT_EXISTS.code, ExceptionDefinition.FILE_PATH_NOT_EXISTS.key, "filePathNotExists");
27         }
28         File file = new File(zipFilePath);
29         if (!file.exists()) {
30             file.mkdirs();
31         }
32         File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
33         if (zipFile.exists()) {
34             // zip文件存在, 将原有文件删除
35             zipFile.delete();
36         }
37         // zip文件不存在, 打包
38         pushZip(sourceFile, sourceFilePath, zipFile);
39     }
40 
41     private static void pushZip(File sourceFile, String sourceFilePath, File zipFile) throws Exception {
42         FileInputStream fis;
43         BufferedInputStream bis = null;
44         FileOutputStream fos;
45         ZipOutputStream zos = null;
46         try {
47             File[] sourceFiles = sourceFile.listFiles();
48             if (null == sourceFiles || sourceFiles.length < 1) {
49                 log.info("{} >>>> is null", sourceFilePath);
50             } else {
51                 fos = new FileOutputStream(zipFile);
52                 zos = new ZipOutputStream(new BufferedOutputStream(fos));
53                 byte[] bytes = new byte[1024 * 10];
54                 for (int i = 0; i < sourceFiles.length; i++) {
55                     // 创建ZIP实体,并添加进压缩包  
56                     ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName());
57                     zos.putNextEntry(zipEntry);
58                     // 读取待压缩的文件并写进压缩包里  
59                     fis = new FileInputStream(sourceFiles[i]);
60                     bis = new BufferedInputStream(fis, 10240);
61                     int read = 0;
62                     while ((read = bis.read(bytes, 0, 10240)) != -1) {
63                         zos.write(bytes, 0, read);
64                     }
65                 }
66             }
67         } catch (IOException e) {
68             e.printStackTrace();
69             throw e;
70         } finally {
71             try {
72                 if (null != bis) {
73                     bis.close();
74                 }
75                 if (null != zos) {
76                     zos.close();
77                 }
78             } catch (IOException e) {
79                 e.printStackTrace();
80             }
81         }
82     }
83 }

 

转载于:https://www.cnblogs.com/yanwu0527/p/9166547.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值