11package org .joychou .controller ;
22
3+ import com .fasterxml .uuid .Generators ;
34import org .springframework .stereotype .Controller ;
45import org .springframework .web .bind .annotation .GetMapping ;
56import org .springframework .web .bind .annotation .PostMapping ;
89import org .springframework .web .multipart .MultipartFile ;
910import org .springframework .web .servlet .mvc .support .RedirectAttributes ;
1011
12+ import java .io .File ;
13+ import java .io .FileOutputStream ;
1114import java .io .IOException ;
1215import java .nio .file .Files ;
1316import java .nio .file .Path ;
1417import java .nio .file .Paths ;
18+ import java .util .UUID ;
19+
20+ import static org .joychou .utils .Security .isImage ;
21+
1522
1623/**
1724 * @author: JoyChou ([email protected] ) @@ -31,6 +38,11 @@ public String index() {
3138 return "upload" ; // return upload.html page
3239 }
3340
41+ @ GetMapping ("/pic" )
42+ public String uploadPic () {
43+ return "uploadPic" ; // return uploadPic.html page
44+ }
45+
3446 @ PostMapping ("/upload" )
3547 public String singleFileUpload (@ RequestParam ("file" ) MultipartFile file ,
3648 RedirectAttributes redirectAttributes ) {
@@ -52,15 +64,94 @@ public String singleFileUpload(@RequestParam("file") MultipartFile file,
5264 } catch (IOException e ) {
5365 redirectAttributes .addFlashAttribute ("message" , "upload failed" );
5466 e .printStackTrace ();
55- return "uploadStatus " ;
67+ return "redirect:/file/status " ;
5668 }
5769
5870 return "redirect:/file/status" ;
5971 }
6072
73+ // only upload picture
74+ @ PostMapping ("/upload/picture" )
75+ public String uploadPicture (@ RequestParam ("file" ) MultipartFile multifile ,
76+ RedirectAttributes redirectAttributes ) throws Exception {
77+ if (multifile .isEmpty ()) {
78+ // 赋值给uploadStatus.html里的动态参数message
79+ redirectAttributes .addFlashAttribute ("message" , "Please select a file to upload" );
80+ return "redirect:/file/status" ;
81+ }
82+
83+ // get suffix
84+ String fileName = multifile .getOriginalFilename ();
85+ String Suffix = fileName .substring (fileName .lastIndexOf ("." ));
86+
87+ File excelFile = convert (multifile );
88+
89+ // security check
90+ String picSuffixList [] = {".jpg" , ".png" , ".jpeg" , ".gif" , ".bmp" };
91+ Boolean suffixFlag = false ;
92+ for (String white_suffix : picSuffixList ) {
93+ if (Suffix .toLowerCase ().equals (white_suffix )) {
94+ suffixFlag = true ;
95+ break ;
96+ }
97+ }
98+ if ( !suffixFlag || !isImage (excelFile ) ) {
99+ redirectAttributes .addFlashAttribute ("message" , "illeagl picture" );
100+ deleteFile (excelFile );
101+ return "redirect:/file/status" ;
102+ }
103+
104+
105+ try {
106+ // Get the file and save it somewhere
107+ byte [] bytes = multifile .getBytes ();
108+ Path path = Paths .get (UPLOADED_FOLDER + multifile .getOriginalFilename ());
109+ Files .write (path , bytes );
110+
111+ redirectAttributes .addFlashAttribute ("message" ,
112+ "You successfully uploaded '" + UPLOADED_FOLDER + multifile .getOriginalFilename () + "'" );
113+
114+ } catch (IOException e ) {
115+ redirectAttributes .addFlashAttribute ("message" , "upload failed" );
116+ e .printStackTrace ();
117+ deleteFile (excelFile );
118+ return "redirect:/file/status" ;
119+ }
120+
121+ deleteFile (excelFile );
122+ return "redirect:/file/status" ;
123+ }
124+
61125 @ GetMapping ("/status" )
62126 public String uploadStatus () {
63127 return "uploadStatus" ;
64128 }
65129
130+ private void deleteFile (File ... files ) {
131+ for (File file : files ) {
132+ if (file .exists ()) {
133+ file .delete ();
134+ }
135+ }
136+ }
137+
138+ /**
139+ * @desc 不建议使用transferTo,因为原始的MultipartFile会被覆盖
140+ * @url https://stackoverflow.com/questions/24339990/how-to-convert-a-multipart-file-to-file
141+ * @param multiFile
142+ * @return
143+ * @throws Exception
144+ */
145+ private File convert (MultipartFile multiFile ) throws Exception {
146+ String fileName = multiFile .getOriginalFilename ();
147+ String suffix = fileName .substring (fileName .lastIndexOf ("." ));
148+ UUID uuid = Generators .timeBasedGenerator ().generate ();
149+
150+ File convFile = new File (UPLOADED_FOLDER + uuid + suffix );
151+ convFile .createNewFile ();
152+ FileOutputStream fos = new FileOutputStream (convFile );
153+ fos .write (multiFile .getBytes ());
154+ fos .close ();
155+ return convFile ;
156+ }
66157}
0 commit comments