31
31
public class FileUpload {
32
32
33
33
// Save the uploaded file to this folder
34
- private static String UPLOADED_FOLDER = "/tmp/" ;
34
+ private static final String UPLOADED_FOLDER = "/tmp/" ;
35
35
private final Logger logger = LoggerFactory .getLogger (this .getClass ());
36
+ private static String randomFilePath = "" ;
36
37
37
- @ GetMapping ("/" )
38
+ // uplaod any file
39
+ @ GetMapping ("/any" )
38
40
public String index () {
39
41
return "upload" ; // return upload.html page
40
42
}
41
43
44
+ // only allow to upload pictures
42
45
@ GetMapping ("/pic" )
43
46
public String uploadPic () {
44
47
return "uploadPic" ; // return uploadPic.html page
@@ -64,13 +67,16 @@ public String singleFileUpload(@RequestParam("file") MultipartFile file,
64
67
65
68
} catch (IOException e ) {
66
69
redirectAttributes .addFlashAttribute ("message" , "upload failed" );
67
- e .printStackTrace ();
68
- return "redirect:/file/status" ;
70
+ logger .error (e .toString ());
69
71
}
70
72
71
73
return "redirect:/file/status" ;
72
74
}
73
75
76
+ @ GetMapping ("/status" )
77
+ public String uploadStatus () {
78
+ return "uploadStatus" ;
79
+ }
74
80
75
81
// only upload picture
76
82
@ PostMapping ("/upload/picture" )
@@ -83,11 +89,12 @@ public String uploadPicture(@RequestParam("file") MultipartFile multifile) throw
83
89
String fileName = multifile .getOriginalFilename ();
84
90
String Suffix = fileName .substring (fileName .lastIndexOf ("." )); // 获取文件后缀名
85
91
String mimeType = multifile .getContentType (); // 获取MIME类型
92
+ String filePath = UPLOADED_FOLDER + fileName ;
86
93
File excelFile = convert (multifile );
87
94
88
95
89
96
// 判断文件后缀名是否在白名单内 校验1
90
- String picSuffixList [] = {".jpg" , ".png" , ".jpeg" , ".gif" , ".bmp" , ".ico" };
97
+ String [] picSuffixList = {".jpg" , ".png" , ".jpeg" , ".gif" , ".bmp" , ".ico" };
91
98
boolean suffixFlag = false ;
92
99
for (String white_suffix : picSuffixList ) {
93
100
if (Suffix .toLowerCase ().equals (white_suffix )) {
@@ -97,13 +104,13 @@ public String uploadPicture(@RequestParam("file") MultipartFile multifile) throw
97
104
}
98
105
if (!suffixFlag ) {
99
106
logger .error ("[-] Suffix error: " + Suffix );
100
- deleteFile (excelFile );
107
+ deleteFile (filePath );
101
108
return "Upload failed. Illeagl picture." ;
102
109
}
103
110
104
111
105
112
// 判断MIME类型是否在黑名单内 校验2
106
- String mimeTypeBlackList [] = {
113
+ String [] mimeTypeBlackList = {
107
114
"text/html" ,
108
115
"text/javascript" ,
109
116
"application/javascript" ,
@@ -115,17 +122,18 @@ public String uploadPicture(@RequestParam("file") MultipartFile multifile) throw
115
122
// 用contains是为了防止text/html;charset=UTF-8绕过
116
123
if (SecurityUtil .replaceSpecialStr (mimeType ).toLowerCase ().contains (blackMimeType )) {
117
124
logger .error ("[-] Mime type error: " + mimeType );
118
- deleteFile (excelFile );
125
+ deleteFile (filePath );
119
126
return "Upload failed. Illeagl picture." ;
120
127
}
121
128
}
122
129
123
130
// 判断文件内容是否是图片 校验3
124
131
boolean isImageFlag = isImage (excelFile );
132
+ deleteFile (randomFilePath );
125
133
126
134
if (!isImageFlag ) {
127
135
logger .error ("[-] File is not Image" );
128
- deleteFile (excelFile );
136
+ deleteFile (filePath );
129
137
return "Upload failed. Illeagl picture." ;
130
138
}
131
139
@@ -137,37 +145,39 @@ public String uploadPicture(@RequestParam("file") MultipartFile multifile) throw
137
145
Files .write (path , bytes );
138
146
} catch (IOException e ) {
139
147
logger .error (e .toString ());
140
- deleteFile (excelFile );
148
+ deleteFile (filePath );
141
149
return "Upload failed" ;
142
150
}
143
151
144
- deleteFile (excelFile );
145
152
logger .info ("[+] Safe file. Suffix: {}, MIME: {}" , Suffix , mimeType );
146
- logger .info ("[+] Successfully uploaded {}{} " , UPLOADED_FOLDER , multifile . getOriginalFilename () );
147
- return "Upload success" ;
153
+ logger .info ("[+] Successfully uploaded {}" , filePath );
154
+ return String . format ( "You successfully uploaded '%s'" , filePath ) ;
148
155
}
149
156
150
- private void deleteFile (File ... files ) {
151
- for (File file : files ) {
152
- if (file .exists ()) {
153
- boolean ret = file .delete ();
154
- if (ret ) {
155
- logger .debug ("File delete successfully!" );
156
- }
157
+ private void deleteFile (String filePath ) {
158
+ File delFile = new File (filePath );
159
+ if (delFile .isFile () && delFile .exists ()) {
160
+ if (delFile .delete ()) {
161
+ logger .info ("[+] " + filePath + " delete successfully!" );
162
+ return ;
157
163
}
158
164
}
165
+ logger .info (filePath + " delete failed!" );
159
166
}
160
167
161
168
/**
169
+ * 为了使用ImageIO.read()
170
+ *
162
171
* 不建议使用transferTo,因为原始的MultipartFile会被覆盖
163
172
* https://stackoverflow.com/questions/24339990/how-to-convert-a-multipart-file-to-file
164
173
*/
165
174
private File convert (MultipartFile multiFile ) throws Exception {
166
175
String fileName = multiFile .getOriginalFilename ();
167
176
String suffix = fileName .substring (fileName .lastIndexOf ("." ));
168
177
UUID uuid = Generators .timeBasedGenerator ().generate ();
169
-
170
- File convFile = new File (UPLOADED_FOLDER + uuid + suffix );
178
+ randomFilePath = UPLOADED_FOLDER + uuid + suffix ;
179
+ // 随机生成一个同后缀名的文件
180
+ File convFile = new File (randomFilePath );
171
181
boolean ret = convFile .createNewFile ();
172
182
if (!ret ) {
173
183
return null ;
@@ -183,6 +193,6 @@ private File convert(MultipartFile multiFile) throws Exception {
183
193
*/
184
194
private static boolean isImage (File file ) throws IOException {
185
195
BufferedImage bi = ImageIO .read (file );
186
- return bi = = null ;
196
+ return bi ! = null ;
187
197
}
188
198
}
0 commit comments