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
package org.rcisoft.common.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* Created with family.
* author: cy
* Date: 16/6/28
* Time: 下午3:28
* description: 文件操作
*/
@Slf4j
@Controller
public class FileController<T> extends PaginationController<T> {
protected static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
protected static final String SEPARATOR;
static {
SEPARATOR = File.separator;
}
protected String globalCommonFileUpload(MultipartFile multipartFile, String savingPatternOrFullName, String savingAbsolutePath) {
String finalSavingPath;
String returnFinalFileName;
label32: {
String fileExtendsName = "." + getSuffix(multipartFile.getOriginalFilename());
finalSavingPath = "";
savingPatternOrFullName = savingPatternOrFullName.toUpperCase();
returnFinalFileName = "";
switch(savingPatternOrFullName.hashCode()) {
case 2090926:
if(savingPatternOrFullName.equals("DATE")) {
returnFinalFileName = getCurrentDateTimeString() + fileExtendsName;
finalSavingPath = savingAbsolutePath + SEPARATOR + getCurrentDateTimeString() + fileExtendsName;
break label32;
}
break;
case 2616251:
if(savingPatternOrFullName.equals("UUID")) {
returnFinalFileName = getUUIDString() + fileExtendsName;
finalSavingPath = savingAbsolutePath + SEPARATOR + getUUIDString() + fileExtendsName;
break label32;
}
}
returnFinalFileName = savingPatternOrFullName;
finalSavingPath = savingAbsolutePath + SEPARATOR + savingPatternOrFullName;
}
// File file = new File(finalSavingPath);
// if(!file.mkdirs()) {
// file.mkdirs();
// }
//
// try {
// multipartFile.transferTo(file);
// } catch (IllegalStateException var10) {
// var10.printStackTrace();
// } catch (IOException var11) {
// var11.printStackTrace();
// }
//
// return returnFinalFileName;
//以下代码修改过
File file = new File(finalSavingPath);
File fileDir = new File(savingAbsolutePath);
if(!fileDir.exists()) {
fileDir.mkdirs();
}
try {
file.deleteOnExit();
file.createNewFile();
multipartFile.transferTo(file);
} catch (IllegalStateException var10) {
var10.printStackTrace();
} catch (IOException var11) {
var11.printStackTrace();
}
//修改过
return finalSavingPath;
}
public static String getSuffix(String filename) {
String suffix = "";
int pos = filename.lastIndexOf(46);
if(pos > 0 && pos < filename.length() - 1) {
suffix = filename.substring(pos + 1);
}
return suffix;
}
protected static String getUUIDString() {
return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
}
protected static String getCurrentDateTimeString() {
return simpleDateFormat.format(new Date());
}
}