@@ -68,6 +68,8 @@ public abstract class CommonsFileUploadSupport {
68
68
69
69
private boolean uploadTempDirSpecified = false ;
70
70
71
+ private boolean preserveFilename = false ;
72
+
71
73
72
74
/**
73
75
* Instantiate a new CommonsFileUploadSupport with its
@@ -174,6 +176,20 @@ protected boolean isUploadTempDirSpecified() {
174
176
return this .uploadTempDirSpecified ;
175
177
}
176
178
179
+ /**
180
+ * Set whether to preserve the filename as sent by the client, not stripping off
181
+ * path information in {@link CommonsMultipartFile#getOriginalFilename()}.
182
+ * <p>Default is "false", stripping off path information that may prefix the
183
+ * actual filename e.g. from Opera. Switch this to "true" for preserving the
184
+ * client-specified filename as-is, including potential path separators.
185
+ * @since 4.3.5
186
+ * @see MultipartFile#getOriginalFilename()
187
+ * @see CommonsMultipartFile#setPreserveFilename(boolean)
188
+ */
189
+ public void setPreserveFilename (boolean preserveFilename ) {
190
+ this .preserveFilename = preserveFilename ;
191
+ }
192
+
177
193
178
194
/**
179
195
* Factory method for a Commons DiskFileItemFactory instance.
@@ -265,7 +281,7 @@ protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String
265
281
}
266
282
else {
267
283
// multipart file field
268
- CommonsMultipartFile file = new CommonsMultipartFile (fileItem );
284
+ CommonsMultipartFile file = createMultipartFile (fileItem );
269
285
multipartFiles .add (file .getName (), file );
270
286
if (logger .isDebugEnabled ()) {
271
287
logger .debug ("Found multipart file [" + file .getName () + "] of size " + file .getSize () +
@@ -277,6 +293,20 @@ protected MultipartParsingResult parseFileItems(List<FileItem> fileItems, String
277
293
return new MultipartParsingResult (multipartFiles , multipartParameters , multipartParameterContentTypes );
278
294
}
279
295
296
+ /**
297
+ * Create a {@link CommonsMultipartFile} wrapper for the given Commons {@link FileItem}.
298
+ * @param fileItem the Commons FileItem to wrap
299
+ * @return the corresponding CommonsMultipartFile (potentially a custom subclass)
300
+ * @since 4.3.5
301
+ * @see #setPreserveFilename(boolean)
302
+ * @see CommonsMultipartFile#setPreserveFilename(boolean)
303
+ */
304
+ protected CommonsMultipartFile createMultipartFile (FileItem fileItem ) {
305
+ CommonsMultipartFile multipartFile = new CommonsMultipartFile (fileItem );
306
+ multipartFile .setPreserveFilename (this .preserveFilename );
307
+ return multipartFile ;
308
+ }
309
+
280
310
/**
281
311
* Cleanup the Spring MultipartFiles created during multipart parsing,
282
312
* potentially holding temporary data on disk.
@@ -323,6 +353,7 @@ protected static class MultipartParsingResult {
323
353
324
354
public MultipartParsingResult (MultiValueMap <String , MultipartFile > mpFiles ,
325
355
Map <String , String []> mpParams , Map <String , String > mpParamContentTypes ) {
356
+
326
357
this .multipartFiles = mpFiles ;
327
358
this .multipartParameters = mpParams ;
328
359
this .multipartParameterContentTypes = mpParamContentTypes ;
0 commit comments