11package  apijson .boot ;
22
33import  java .io .*;
4- import  java .util .ArrayList ;
5- import  java .util .Arrays ;
6- import  java .util .List ;
7- import  java .util .Objects ;
4+ import  java .net .URLEncoder ;
5+ import  java .text .DateFormat ;
6+ import  java .util .*;
87
98//import javax.annotation.PostConstruct; 
109
10+ import  apijson .ExcelUtil ;
11+ import  apijson .StringUtil ;
1112import  org .apache .commons .io .FileUtils ;
1213import  org .springframework .core .io .InputStreamResource ;
1314import  org .springframework .http .HttpHeaders ;
2526
2627import  apijson .demo .DemoParser ;
2728
29+ import  static  com .google .common .io .Files .getFileExtension ;
30+ 
2831/**文件相关的控制器,包括上传、下载、浏览等 
2932 * @author : ramostear 
3033 * @modifier : Lemon 
@@ -104,18 +107,21 @@ public boolean accept(File file) {
104107@ ResponseBody 
105108public  JSONObject  upload (@ RequestParam ("file" ) MultipartFile  file ) {
106109try  {
107- File  convertFile  = new  File (fileUploadRootDir  + file .getOriginalFilename ());
110+ String  name  = file .getOriginalFilename ();
111+ name  = (StringUtil .isEmpty (name ) ? DateFormat .getDateInstance ().format (new  Date ()) : name )
112+ .replaceAll ("[^a-zA-Z0-9._-]" , String .valueOf (Math .round (1100 *Math .random ())));
113+ File  convertFile  = new  File (fileUploadRootDir  + name );
108114FileOutputStream  fileOutputStream ;
109115fileOutputStream  = new  FileOutputStream (convertFile );
110116fileOutputStream .write (file .getBytes ());
111117fileOutputStream .close ();
112118
113119if  (fileNames  != null  && ! fileNames .isEmpty ()) {
114- fileNames .add (file . getOriginalFilename () );
120+ fileNames .add (name );
115121}
116122
117123JSONObject  res  = new  JSONObject ();
118- res .put ("path" , "/download/"  + file . getOriginalFilename () );
124+ res .put ("path" , "/download/"  + name );
119125res .put ("size" , file .getBytes ().length );
120126return  new  DemoParser ().extendSuccessResult (res );
121127} 
@@ -132,40 +138,76 @@ public ResponseEntity<Object> download(@PathVariable(name = "fileName") String f
132138File  file  = new  File (fileUploadRootDir  + fileName );
133139InputStreamResource  resource  = new  InputStreamResource (new  FileInputStream (file ));
134140
141+ String  encodedFileName  = fileName ;
142+ try  {
143+ encodedFileName  = URLEncoder .encode (fileName , StringUtil .UTF_8 );
144+ } catch  (UnsupportedEncodingException  e ) {
145+ e .printStackTrace ();
146+ }
147+ 
135148HttpHeaders  headers  = new  HttpHeaders ();
136- headers .add ("Content-Disposition" , String .format ("attachment;filename=\" %s" , fileName ));
137- headers .add ("Cache-Control" , "no-cache,no-store,must-revalidate" );
138- headers .add ("Pragma" , "no-cache" );
139- headers .add ("Expires" , "0" );
149+ headers .add ("Content-Disposition" , String .format ("attachment;filename=\" %s;filename*=UTF_8''%s" , fileName , encodedFileName ));
150+ headers .add ("Cache-Control" , "public, max-age=86400" );
151+ //	headers.add("Cache-Control", "no-cache,no-store,must-revalidate"); 
152+ //	headers.add("Pragma", "no-cache"); 
153+ //	headers.add("Expires", "0"); 
140154
141155ResponseEntity <Object > responseEntity  = ResponseEntity .ok ()
142156.headers (headers )
143157.contentLength (file .length ())
144- .contentType (MediaType . parseMediaType ( "application/txt" ))
158+ .contentType (determineContentType ( fileName ))
145159.body (resource );
146160
147161return  responseEntity ;
148162}
149163
164+ private  MediaType  determineContentType (String  fileName ) {
165+ String  extension  = getFileExtension (fileName ).toLowerCase ();
166+ switch  (extension ) {
167+ case  "jpg" :
168+ case  "jpeg" :
169+ return  MediaType .IMAGE_JPEG ;
170+ case  "png" :
171+ return  MediaType .IMAGE_PNG ;
172+ case  "gif" :
173+ return  MediaType .IMAGE_GIF ;
174+ case  "pdf" :
175+ return  MediaType .APPLICATION_PDF ;
176+ case  "json" :
177+ return  MediaType .APPLICATION_JSON ;
178+ case  "xml" :
179+ return  MediaType .APPLICATION_XML ;
180+ case  "txt" :
181+ return  MediaType .TEXT_PLAIN ;
182+ case  "css" :
183+ return  MediaType .valueOf ("text/css" );
184+ case  "js" :
185+ return  MediaType .valueOf ("application/javascript" );
186+ default :
187+ return  MediaType .APPLICATION_OCTET_STREAM ;
188+ }
189+ }
190+ 
150191@ GetMapping ("/download/report/{reportId}/cv" )
151192@ ResponseBody 
152193public  ResponseEntity <Object > downloadCVReport (@ PathVariable (name  = "reportId" ) String  reportId ) throws  FileNotFoundException , IOException  {
153194String  name  = "CVAuto_report_"  + reportId  + ".xlsx" ;
154- File  file  = new  File (fileUploadRootDir  + name );
195+ String  path  = fileUploadRootDir  + name ;
196+ File  file  = new  File (path );
155197long  size  = file .exists () ? file .length () : 0 ;
156198if  (size  < 10 *1024 ) {
157199file .delete ();
158200}
159201
160202if  ((file .exists () ? file .length () : 0 ) < 10 *1024 ) {
161203String  filePath  = ExcelUtil .newCVAutoReportWithTemplate (fileUploadRootDir , name );
162- if  (! Objects .equals (filePath , fileUploadRootDir  +  name )) {
204+ if  (! Objects .equals (filePath , path )) {
163205try  {
164206File  sourceFile  = new  File (filePath );
165- File  destFile  = new  File (fileUploadRootDir  +  name );
207+ File  destFile  = new  File (path );
166208if  (! destFile .getAbsolutePath ().equals (sourceFile .getAbsolutePath ())) {
167209FileUtils .copyFile (sourceFile , destFile );
168- System .out .println ("文件复制完成 (Commons IO): "  + filePath  + " -> "  + fileUploadRootDir  +  name );
210+ System .out .println ("文件复制完成 (Commons IO): "  + filePath  + " -> "  + path );
169211}
170212} catch  (IOException  e ) {
171213e .printStackTrace ();
0 commit comments