1
1
package apijson .boot ;
2
2
3
3
import 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 .*;
8
7
9
8
//import javax.annotation.PostConstruct;
10
9
10
+ import apijson .ExcelUtil ;
11
+ import apijson .StringUtil ;
11
12
import org .apache .commons .io .FileUtils ;
12
13
import org .springframework .core .io .InputStreamResource ;
13
14
import org .springframework .http .HttpHeaders ;
25
26
26
27
import apijson .demo .DemoParser ;
27
28
29
+ import static com .google .common .io .Files .getFileExtension ;
30
+
28
31
/**文件相关的控制器,包括上传、下载、浏览等
29
32
* @author : ramostear
30
33
* @modifier : Lemon
@@ -104,18 +107,21 @@ public boolean accept(File file) {
104
107
@ ResponseBody
105
108
public JSONObject upload (@ RequestParam ("file" ) MultipartFile file ) {
106
109
try {
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 );
108
114
FileOutputStream fileOutputStream ;
109
115
fileOutputStream = new FileOutputStream (convertFile );
110
116
fileOutputStream .write (file .getBytes ());
111
117
fileOutputStream .close ();
112
118
113
119
if (fileNames != null && ! fileNames .isEmpty ()) {
114
- fileNames .add (file . getOriginalFilename () );
120
+ fileNames .add (name );
115
121
}
116
122
117
123
JSONObject res = new JSONObject ();
118
- res .put ("path" , "/download/" + file . getOriginalFilename () );
124
+ res .put ("path" , "/download/" + name );
119
125
res .put ("size" , file .getBytes ().length );
120
126
return new DemoParser ().extendSuccessResult (res );
121
127
}
@@ -132,40 +138,76 @@ public ResponseEntity<Object> download(@PathVariable(name = "fileName") String f
132
138
File file = new File (fileUploadRootDir + fileName );
133
139
InputStreamResource resource = new InputStreamResource (new FileInputStream (file ));
134
140
141
+ String encodedFileName = fileName ;
142
+ try {
143
+ encodedFileName = URLEncoder .encode (fileName , StringUtil .UTF_8 );
144
+ } catch (UnsupportedEncodingException e ) {
145
+ e .printStackTrace ();
146
+ }
147
+
135
148
HttpHeaders 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");
140
154
141
155
ResponseEntity <Object > responseEntity = ResponseEntity .ok ()
142
156
.headers (headers )
143
157
.contentLength (file .length ())
144
- .contentType (MediaType . parseMediaType ( "application/txt" ))
158
+ .contentType (determineContentType ( fileName ))
145
159
.body (resource );
146
160
147
161
return responseEntity ;
148
162
}
149
163
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
+
150
191
@ GetMapping ("/download/report/{reportId}/cv" )
151
192
@ ResponseBody
152
193
public ResponseEntity <Object > downloadCVReport (@ PathVariable (name = "reportId" ) String reportId ) throws FileNotFoundException , IOException {
153
194
String name = "CVAuto_report_" + reportId + ".xlsx" ;
154
- File file = new File (fileUploadRootDir + name );
195
+ String path = fileUploadRootDir + name ;
196
+ File file = new File (path );
155
197
long size = file .exists () ? file .length () : 0 ;
156
198
if (size < 10 *1024 ) {
157
199
file .delete ();
158
200
}
159
201
160
202
if ((file .exists () ? file .length () : 0 ) < 10 *1024 ) {
161
203
String filePath = ExcelUtil .newCVAutoReportWithTemplate (fileUploadRootDir , name );
162
- if (! Objects .equals (filePath , fileUploadRootDir + name )) {
204
+ if (! Objects .equals (filePath , path )) {
163
205
try {
164
206
File sourceFile = new File (filePath );
165
- File destFile = new File (fileUploadRootDir + name );
207
+ File destFile = new File (path );
166
208
if (! destFile .getAbsolutePath ().equals (sourceFile .getAbsolutePath ())) {
167
209
FileUtils .copyFile (sourceFile , destFile );
168
- System .out .println ("文件复制完成 (Commons IO): " + filePath + " -> " + fileUploadRootDir + name );
210
+ System .out .println ("文件复制完成 (Commons IO): " + filePath + " -> " + path );
169
211
}
170
212
} catch (IOException e ) {
171
213
e .printStackTrace ();
0 commit comments