Skip to content

Document how MultiPartFile.transferTo works with Servlet 3 [SPR-12650] #17251

@spring-projects-issues

Description

@spring-projects-issues

Martin Sivák opened SPR-12650 and commented

When a multipart file upload is attempted using Spring MVC, MultiPartFile object is received.

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public EmberModel upload(@RequestParam("files[]") MultipartFile[] files) {

The documentation (http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/multipart/MultipartFile.html#transferTo-java.io.File-) then says that the transferTo(File destination) method will move the uploaded file to the provided location.

Well on Jetty 9 it won't..

The reason for this is hidden in org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart#write(java.lang.String) which tries to do the following (the second codepath does that in the same way):

File f = new File(_tmpDir, fileName);
if (_file.renameTo(f))
    _file = f;

The filename argument bubbles there from:

// org/springframework/web/multipart/support/StandardMultipartHttpServletRequest.java
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
	this.part.write(dest.getPath());
}

The destination itself is correct, but the File(parent, path) constructor will always convert it to relative path and prefix it with the _tmpDir. That will convert path like /home/myuser/destination.txt to /tmp/home/myuser/destination.txt on my system.

Now this might be Jetty's fault, but it essentially means that the transferTo method is useless for Jetty backed application, because there is no way to give it an absolute path.


Affects: 4.1.4

Issue Links:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions