MultipartFileToFile.java
11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.idss.vulsync.utils;
import cloud.agileframework.common.util.file.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
@Slf4j
public class MultipartFileToFile {
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file, String rename) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(rename);
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
/**
* MultipartFile 转 File
*
* @param file
* @throws Exception
*/
public static File multipartFileToFile(MultipartFile file, String path, String rename) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
isFileDir(path + "/");
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(path + "/" + rename);
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
public static void isFileDir(String path) {
String osName = System.getProperty("os.name");
//兼容 linux 系统
if (osName.toLowerCase().startsWith("win")) {
path = path.replaceAll("/", "\\\\");
}
//判断路径是否存在
File filePath = new File(path);
if (!filePath.exists()) {
//不存在,创建目录
filePath.mkdirs();
}
}
//获取流文件
public static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除本地临时文件
*
* @param file
*/
public static void deleteTempFile(File file) {
if (file != null) {
File del = new File(file.toURI());
del.delete();
}
}
/**
* 删除本地临时文件
*
* @param multipartFile
*/
public static void deleteTempFile(MultipartFile multipartFile) {
try {
File file = multipartFileToFile(multipartFile);
if (file != null) {
File del = new File(file.toURI());
del.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将zip文件解压成file集
*
* @param multipartFile
* @return
* @throws IOException
*/
public static List<MultipartFile> zipToFiles(MultipartFile multipartFile) {
//获取文件输入流
InputStream input = null;
ZipInputStream zipInputStream = null;
try {
input = multipartFile.getInputStream();
//获取ZIP输入流(一定要指定字符集Charset.forName("GBK")否则会报java.lang.IllegalArgumentException: MALFORMED)
zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));
ZipFile zf = toFile(multipartFile);
//定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
ZipEntry ze = null;
List<MultipartFile> list = new ArrayList<>();
//循环遍历
while ((ze = zipInputStream.getNextEntry()) != null) {
InputStream is = zf.getInputStream(ze);
MultipartFile file = getMultipartFile(is, ze.getName());
list.add(file);
}
//一定记得关闭流
zipInputStream.closeEntry();
input.close();
return list;
} catch (Exception e) {
try {
zipInputStream.closeEntry();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
public static List<MultipartFile> zipParse(InputStream input) {
ZipInputStream zipInputStream = null;
try {
//将包含压缩包信息的字节数组转化为zipInputStream
zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));
ZipEntry ze = null;
List<MultipartFile> list = new ArrayList<>();
while ((ze = zipInputStream.getNextEntry()) != null) {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
IOUtils.copy(zipInputStream, byteOut);
InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray());
MultipartFile mFile = getMultipartFile(inputStream, ze.getName());
list.add(mFile);
byteOut.close();
inputStream.close();
}
zipInputStream.closeEntry();
input.close();
return list;
} catch (Exception e) {
try {
zipInputStream.closeEntry();
input.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
/**
* 获取封装得MultipartFile
*
* @param inputStream inputStream
* @param fileName fileName
* @return MultipartFile
*/
public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
FileItem fileItem = createFileItem(inputStream, fileName);
//CommonsMultipartFile是feign对multipartFile的封装,但是要FileItem类对象
return new CommonsMultipartFile(fileItem);
}
/**
* FileItem类对象创建
*
* @param inputStream inputStream
* @param fileName fileName
* @return FileItem
*/
public static FileItem createFileItem(InputStream inputStream, String fileName) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "file";
FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
OutputStream os = null;
//使用输出流输出输入流的字节
try {
os = item.getOutputStream();
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
} catch (IOException e) {
throw new IllegalArgumentException("文件上传失败");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
return item;
}
private static ZipFile toFile(MultipartFile multipartFile) throws Exception {
if (multipartFile == null || multipartFile.getSize() <= 0) {
return null;
}
File file = multipartFileToFile(multipartFile);
if (file == null || !file.exists()) {
return null;
}
ZipFile zipFile = new ZipFile(file, Charset.forName("GBK"));
return zipFile;
}
/**
* 文件内容写入文件
*
* @param zipFileName 文件名不含序号
*/
public static void writeToFile(File contentLogFile,String zipDir, String zipFileName) {
//创建文件集合
List<File> fileList = new ArrayList<>();
fileList.add(contentLogFile);
try {
//压缩文件
FileUtil.createZip(fileList, zipDir, zipFileName);
} catch (Exception e) {
log.error("压缩文件失败", e);
}
}
public static List<String> splitZip(String zipDir,String zipName, int chunkSize, String outputDirectory) throws IOException {
List<String> zipNameList=new ArrayList<>();
String inputZip=zipDir+"/"+zipName;
try (ZipFile zipFile = new ZipFile(inputZip+".zip")) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
int partNumber = 1;
int currentSize = 0;
ZipOutputStream zos = null;
FileOutputStream fos = null;
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (zos == null || currentSize >= chunkSize) {
if (zos != null) {
zos.close();
}
String partFileName = getPartFileName(zipName, partNumber++,outputDirectory);
fos = new FileOutputStream(partFileName);
zos = new ZipOutputStream(fos);
currentSize = 0;
zipNameList.add(partFileName);
}
zos.putNextEntry(new ZipEntry(entry.getName()));
try (InputStream is = zipFile.getInputStream(entry)) {
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) > 0) {
zos.write(buffer, 0, len);
currentSize += len;
}
}
zos.closeEntry();
}
if (zos != null) {
zos.close();
}
}
return zipNameList;
}
private static String getPartFileName(String zipName, int partNumber,String outputDirectory) {
return outputDirectory+File.separator+zipName+"_"+String.format("%04d", partNumber)+".zip";
}
}