• 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 }
  • 相关阅读:
    opencv 读写图像文件
    opencv 图像透明
    c# 异步事件
    c++读写文件
    Delphi XE UniGUI UniSession文件下载的方式(SendFile、SendStream )
    Delphi XE UniGUI ExtJS [8] 自定义JavaScript文件调用
    Delphi XE UniGUI ExtJS [7] Delhi 动态添加 ClientEvents.ExtEvents 事件
    Delphi XE UniGUI ExtJS [6] fireEvent 事件 模拟按键点击(JS、ExtJS、Delphi)
    Delphi XE UniGUI ExtJS [5] 鼠标和Key事件
    Delphi XE UniGUI ExtJS [4] Ajax、Response(响应) 的使用
  • 原文地址:https://www.cnblogs.com/yanwu0527/p/9166547.html
Copyright © 2020-2023  润新知