Преглед на файлове

压缩包: 压缩包形式批量下载文件

baichengfei преди 4 години
родител
ревизия
f9e88d815b

+ 1 - 0
.gitignore

@@ -141,3 +141,4 @@ Desktop.ini
 # ESLint
 ######################
 .eslintcache
+*.zip

+ 5 - 0
pom.xml

@@ -130,6 +130,11 @@
             <groupId>io.dropwizard.metrics</groupId>
             <artifactId>metrics-annotation</artifactId>
         </dependency>
+        <dependency>
+            <groupId>net.lingala.zip4j</groupId>
+            <artifactId>zip4j</artifactId>
+            <version>1.3.2</version>
+        </dependency>
         <dependency>
             <groupId>io.dropwizard.metrics</groupId>
             <artifactId>metrics-json</artifactId>

+ 29 - 0
src/main/java/com/common/workflow/service/dto/FileDto.java

@@ -0,0 +1,29 @@
+package com.common.workflow.service.dto;
+
+/**
+ * @Author baiChFei
+ * @Email 738983525@qq.com
+ * @Date 2021/8/12 下午5:31
+ */
+public class FileDto {
+    private String url;
+
+    private String name;
+
+    public String getUrl() {
+        return url;
+    }
+
+    public void setUrl(String url) {
+        this.url = url;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+}

+ 60 - 0
src/main/java/com/common/workflow/service/util/File2Zip.java

@@ -0,0 +1,60 @@
+package com.common.workflow.service.util;
+
+import com.common.workflow.service.dto.FileDto;
+import net.lingala.zip4j.core.ZipFile;
+import net.lingala.zip4j.exception.ZipException;
+import net.lingala.zip4j.model.ZipParameters;
+import net.lingala.zip4j.util.Zip4jConstants;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.List;
+
+/**
+ * https://mvnrepository.com/artifact/net.lingala.zip4j/zip4j
+ * https://www.jianshu.com/p/89bf65317e6b
+ * @Author baiChFei
+ * @Email 738983525@qq.com
+ * @Date 2021/8/12 下午7:55
+ */
+public class File2Zip {
+    public static void fileZip(List<FileDto> fileUrls, String zipName,  HttpServletResponse response) {
+        String zipUrl = "./" + zipName;
+        for (FileDto fileDto : fileUrls) {
+            addFileZip(fileDto.getUrl(), fileDto.getName(), zipUrl);
+        }
+
+        // 下载
+    }
+
+    private static void addFileZip(String fileUrl, String fileName, String zipAddr) {
+        InputStream fileInputStream = null;
+        try {
+            URL url = new URL(fileUrl);
+            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+            //得到输入流
+            fileInputStream = conn.getInputStream();
+
+            ZipFile zip = new ZipFile(zipAddr);
+            ZipParameters para = new ZipParameters();
+            para.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
+            para.setFileNameInZip(fileName);
+            para.setSourceExternalStream(true);
+            // 以流的方式添加文件
+            zip.addStream(fileInputStream, para);
+        } catch (IOException | ZipException e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                assert fileInputStream != null;
+                fileInputStream.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+
+    }
+}

+ 9 - 0
src/main/java/com/common/workflow/web/rest/AposeResource.java

@@ -1,7 +1,9 @@
 package com.common.workflow.web.rest;
 
 import com.common.workflow.service.activiti.ActivitiService;
+import com.common.workflow.service.dto.FileDto;
 import com.common.workflow.service.util.ExcelToPdf;
+import com.common.workflow.service.util.File2Zip;
 import com.common.workflow.service.util.Word2Pdf;
 import com.common.workflow.web.rest.vm.AposeVM;
 import org.slf4j.Logger;
@@ -13,6 +15,8 @@ import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.io.IOException;
+import java.util.List;
 
 /**
  * Created by gyue on 2019-01-21.
@@ -40,4 +44,9 @@ public class AposeResource {
         Word2Pdf.word2PdfWithWatermark(aposeVM.getWatermark(), aposeVM.getAddressUrl(), response);
     }
 
+    @PostMapping("/zip")
+    public void Zip(@Valid @RequestBody List<FileDto> fileUrls, HttpServletResponse response) {
+        File2Zip.fileZip(fileUrls, "test.zip", response);
+    }
+
 }