|
@@ -34,54 +34,29 @@ 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);
|
46
|
|
- ZipParameters zipParameters = new ZipParameters();
|
47
|
|
-
|
48
|
|
- for (RFile f : files) {
|
49
|
|
- zipParameters.setFileNameInZip(f.getName());
|
50
|
|
- zos.putNextEntry(zipParameters);
|
51
|
|
- byte[] data = httpUtils.download(f.getUrl());
|
52
|
|
- zos.write(data);
|
53
|
|
- zos.closeEntry();
|
54
|
|
- }
|
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;
|
72
|
|
- }
|
73
|
|
-
|
74
|
|
- public void flush(HttpServletResponse response, ZipFile zipFile) throws IOException {
|
|
37
|
+ public void flush(HttpServletResponse response, List<RFile> files) throws IOException {
|
75
|
38
|
try {
|
76
|
|
- String fileName = zipFile.getFile().getName();
|
77
|
|
- InputStream inputStream = new FileInputStream(zipFile.getFile());
|
|
39
|
+ if (files == null || files.size() < 1) {
|
|
40
|
+ throw new Exception("导出内容为空");
|
|
41
|
+ }
|
78
|
42
|
|
|
43
|
+ String fileName = today() + ".zip";
|
79
|
44
|
response.setContentType("application/zip");
|
80
|
45
|
response.setCharacterEncoding("utf-8");
|
81
|
46
|
response.setHeader("Content-disposition", "attachment;filename="+StringUtils.urlEncode(fileName));
|
82
|
47
|
|
83
|
|
- response.setContentLength(zipFile.getBufferSize());
|
84
|
|
- copyStream(inputStream, response.getOutputStream());
|
|
48
|
+ ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
|
|
49
|
+ ZipParameters zipParameters = new ZipParameters();
|
|
50
|
+
|
|
51
|
+ for (RFile f : files) {
|
|
52
|
+ zipParameters.setFileNameInZip(f.getName());
|
|
53
|
+ zos.putNextEntry(zipParameters);
|
|
54
|
+ byte[] data = httpUtils.download(f.getUrl());
|
|
55
|
+ zos.write(data);
|
|
56
|
+ zos.closeEntry();
|
|
57
|
+ }
|
|
58
|
+ zos.close();
|
|
59
|
+ response.flushBuffer();
|
85
|
60
|
} catch (Exception e) {
|
86
|
61
|
e.printStackTrace();
|
87
|
62
|
|