@@ -62,6 +62,8 @@ public abstract class CommonsFileUploadSupport {
62
62
63
63
private boolean uploadTempDirSpecified = false ;
64
64
65
+ private boolean preserveFilename = false ;
66
+
65
67
66
68
/**
67
69
* Instantiate a new CommonsFileUploadSupport with its
@@ -168,6 +170,20 @@ protected boolean isUploadTempDirSpecified() {
168
170
return this .uploadTempDirSpecified ;
169
171
}
170
172
173
+ /**
174
+ * Set whether to preserve the filename as sent by the client, not stripping off
175
+ * path information in {@link CommonsMultipartFile#getOriginalFilename()}.
176
+ * <p>Default is "false", stripping off path information that may prefix the
177
+ * actual filename e.g. from Opera. Switch this to "true" for preserving the
178
+ * client-specified filename as-is, including potential path separators.
179
+ * @since 4.3.5
180
+ * @see MultipartFile#getOriginalFilename()
181
+ * @see CommonsMultipartFile#setPreserveFilename(boolean)
182
+ */
183
+ public void setPreserveFilename (boolean preserveFilename ) {
184
+ this .preserveFilename = preserveFilename ;
185
+ }
186
+
171
187
172
188
/**
173
189
* Factory method for a Commons DiskFileItemFactory instance.
@@ -259,7 +275,7 @@ protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String
259
275
}
260
276
else {
261
277
// multipart file field
262
- CommonsMultipartFile file = new CommonsMultipartFile (fileItem );
278
+ CommonsMultipartFile file = createMultipartFile (fileItem );
263
279
multipartFiles .add (file .getName (), file );
264
280
if (logger .isDebugEnabled ()) {
265
281
logger .debug ("Found multipart file [" + file .getName () + "] of size " + file .getSize () +
@@ -271,6 +287,20 @@ protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String
271
287
return new MultipartParsingResult (multipartFiles , multipartParameters , multipartParameterContentTypes );
272
288
}
273
289
290
+ /**
291
+ * Create a {@link CommonsMultipartFile} wrapper for the given Commons {@link FileItem}.
292
+ * @param fileItem the Commons FileItem to wrap
293
+ * @return the corresponding CommonsMultipartFile (potentially a custom subclass)
294
+ * @since 4.3.5
295
+ * @see #setPreserveFilename(boolean)
296
+ * @see CommonsMultipartFile#setPreserveFilename(boolean)
297
+ */
298
+ protected CommonsMultipartFile createMultipartFile (FileItem fileItem ) {
299
+ CommonsMultipartFile multipartFile = new CommonsMultipartFile (fileItem );
300
+ multipartFile .setPreserveFilename (this .preserveFilename );
301
+ return multipartFile ;
302
+ }
303
+
274
304
/**
275
305
* Cleanup the Spring MultipartFiles created during multipart parsing,
276
306
* potentially holding temporary data on disk.
@@ -317,6 +347,7 @@ protected static class MultipartParsingResult {
317
347
318
348
public MultipartParsingResult (MultiValueMap <String , MultipartFile > mpFiles ,
319
349
Map <String , String []> mpParams , Map <String , String > mpParamContentTypes ) {
350
+
320
351
this .multipartFiles = mpFiles ;
321
352
this .multipartParameters = mpParams ;
322
353
this .multipartParameterContentTypes = mpParamContentTypes ;
0 commit comments