浏览代码

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

张延森 3 年前
父节点
当前提交
a6b9ae83e5

+ 10
- 32
src/main/java/com/njyunzhi/pet_identity/common/ZipUtil.java 查看文件

34
         return LocalDateTime.now(ZoneId.of("Asia/Shanghai")).format(formatter);
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
         ZipParameters zipParameters = new ZipParameters();
39
         ZipParameters zipParameters = new ZipParameters();
47
 
40
 
48
         for (RFile f : files) {
41
         for (RFile f : files) {
52
             zos.write(data);
45
             zos.write(data);
53
             zos.closeEntry();
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
         try {
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
             response.setContentType("application/zip");
58
             response.setContentType("application/zip");
80
             response.setCharacterEncoding("utf-8");
59
             response.setCharacterEncoding("utf-8");
81
             response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName));
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
         } catch (Exception e) {
63
         } catch (Exception e) {
86
             e.printStackTrace();
64
             e.printStackTrace();
87
 
65
 

+ 1
- 2
src/main/java/com/njyunzhi/pet_identity/controller/TaCardNoController.java 查看文件

116
             }
116
             }
117
         }
117
         }
118
 
118
 
119
-        ZipFile data = zipUtil.zipLink(files);
120
-        zipUtil.flush(response, data);
119
+        zipUtil.flush(response, files);
121
         return null;
120
         return null;
122
     }
121
     }
123
 
122