these are my codes:
public class FileUploadController {
@PostMapping(value = "/pic", consumes = "multipart/form-data")
public String uploadPic(MultipartFile file) throws IOException {
String uploadFileName = file.getOriginalFilename();
File dest = new File(uploadFileName);
file.transferTo(dest);
return dest.getAbsolutePath();
}
}
when the upload is finish, i can't find the file. is there a mistake in the following codes? a character '!' is missing before the code 'dest.isAbsoulte()'?
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {
// ...
public void transferTo(File dest) throws IOException, IllegalStateException {
this.part.write(dest.getPath());
// i think mybe the right code is "if (!dest.isAbsolute() && !dest.exists())".
if (dest.isAbsolute() && !dest.exists()) {
// Servlet 3.0 Part.write is not guaranteed to support absolute file paths:
// may translate the given path to a relative location within a temp dir
// (e.g. on Jetty whereas Tomcat and Undertow detect absolute paths).
// At least we offloaded the file from memory storage; it'll get deleted
// from the temp dir eventually in any case. And for our user's purposes,
// we can manually copy it to the requested location as a fallback.
FileCopyUtils.copy(this.part.getInputStream(), Files.newOutputStream(dest.toPath()));
}
}
}
Comment From: poutsma
Since you supplied a relative File
to MultipartFile::transferTo
, the file is moved to a web-container-specific location. If you did not override the configuration in your container, it will be in the container's temp directory.
If you want to store the file elsewhere, you will need to supply an absolute file, instead of a relative one.
Does this solve your problem?
Comment From: nfdc1986
i think if an absolute file is indispensable, we should throw an exception to remind users, when they provide a relative one.
As shown in the folling pic. If the character "!" is added, the relative path is also ok. Otherwise the 'if' statement will not be called forevery! Am I right?
Comment From: poutsma
i think if an absolute file is indispensable, we should throw an exception to remind users, when they provide a relative one.
You misunderstood. It's perfectly fine to pass a relative file as parameter to MultipartFile::transferTo
, but the result is that these are stored in the container-specific storage directory. See also the the Part::write javadoc. This is exactly what some users want, so I see no reason to throw an exception.
As for the code snipped from StandardMultipartFile::transferTo
: I believe that code is there because not all containers support absolute file location. See #19822 for more details on why we made that change. Since you are passing a non-absolute file, the issue does not appear related.
Again, I believe the file is stored in your container's temp directory. If you're using Spring Boot, it will be in a tomcat.xxxxxxxxx.8080
directory in $TMP
. Have you checked that location?
Comment From: nfdc1986
You are right, the upload file is actually in that location(/tmp/tomcat.***/work/Tomcat/localhost/ROOT). Thank you for the reply. Have a nice day.