Browse Source

Merge branch 'master' of http://git.ycjcjy.com/pet_identity/service

张延森 3 years ago
parent
commit
a6b9ae83e5

+ 10
- 32
src/main/java/com/njyunzhi/pet_identity/common/ZipUtil.java View File

@@ -34,15 +34,8 @@ public class ZipUtil {
34 34
         return LocalDateTime.now(ZoneId.of("Asia/Shanghai")).format(formatter);
35 35
     }
36 36
 
37
-    public ZipFile zipLink(List<RFile> files) throws IOException {
38
-        ZipFile zipFile = new ZipFile(today() + ".zip");
39
-
40
-        if (files == null || files.size() < 1) {
41
-            return zipFile;
42
-        }
43
-
44
-        FileOutputStream fos = new FileOutputStream(zipFile.getFile());
45
-        ZipOutputStream zos = new ZipOutputStream(fos);
37
+    private void zipStream(List<RFile> files, OutputStream outputStream) throws IOException {
38
+        ZipOutputStream zos = new ZipOutputStream(outputStream);
46 39
         ZipParameters zipParameters = new ZipParameters();
47 40
 
48 41
         for (RFile f : files) {
@@ -52,36 +45,21 @@ public class ZipUtil {
52 45
             zos.write(data);
53 46
             zos.closeEntry();
54 47
         }
55
-
56
-        return zipFile;
57
-    }
58
-
59
-    private long copyStream(InputStream input, OutputStream output) throws IOException {
60
-        ReadableByteChannel inputChannel = Channels.newChannel(input);
61
-        WritableByteChannel outputChannel = Channels.newChannel(output);
62
-        ByteBuffer buffer = ByteBuffer.allocateDirect(10240);
63
-        long size = 0;
64
-
65
-        while (inputChannel.read(buffer) != -1) {
66
-            buffer.flip();
67
-            size += outputChannel.write(buffer);
68
-            buffer.clear();
69
-        }
70
-
71
-        return size;
48
+        zos.close();
72 49
     }
73 50
 
74
-    public void flush(HttpServletResponse response, ZipFile zipFile) throws IOException {
51
+    public void flush(HttpServletResponse response, List<RFile> files) throws IOException {
75 52
         try {
76
-            String fileName = zipFile.getFile().getName();
77
-            InputStream inputStream = new FileInputStream(zipFile.getFile());
53
+            if (files == null || files.size() < 1) {
54
+                throw new Exception("导出内容为空");
55
+            }
78 56
 
57
+            String fileName = today() + ".zip";
79 58
             response.setContentType("application/zip");
80 59
             response.setCharacterEncoding("utf-8");
81 60
             response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName));
82
-
83
-            response.setContentLength(zipFile.getBufferSize());
84
-            copyStream(inputStream, response.getOutputStream());
61
+            zipStream(files, response.getOutputStream());
62
+            response.flushBuffer();
85 63
         } catch (Exception e) {
86 64
             e.printStackTrace();
87 65
 

+ 1
- 2
src/main/java/com/njyunzhi/pet_identity/controller/TaCardNoController.java View File

@@ -116,8 +116,7 @@ public class TaCardNoController extends BaseController {
116 116
             }
117 117
         }
118 118
 
119
-        ZipFile data = zipUtil.zipLink(files);
120
-        zipUtil.flush(response, data);
119
+        zipUtil.flush(response, files);
121 120
         return null;
122 121
     }
123 122