Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-issuemaster opened this issue Jan 21, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Jan 21, 2015

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:

  • #19822 StandardMultipartFile.transferTo should fall back to manual copy if Part.write doesn't support absolute locations (e.g. on Jetty)
@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 26, 2015

Rossen Stoyanchev commented

The Javadoc for Part.write says "The file is created relative to the location as specified in the MultipartConfig". I think you need to configure that. This can be confirmed in Jetty's MultiPartInputStreamParser which initializes the _tmp variable from _config.getLocation() or otherwise falls back on "java.io.tmpdir".

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 26, 2015

Martin Sivák commented

Ok, the Part.write doc really does say that. But that means the documentation for transferTo should be updated accordingly. The relative semantics is not visible until you dig into the code.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 27, 2015

Rossen Stoyanchev commented

Yes, the Javadoc can be improved to point in that direction. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.