Reputation: 1097
I tried to upload a file and my service invokes a spring @Async method and passes a file object.
fileAsyncProcessor.process(file);
After this, the file object is becoming null in the process method
public class FileAsyncProcessor {
@Async
public void process(MultipartFile file) {
log.debug("processing file...");
InputStream is = file.getInputStream();
//.....
}
}
file.getInputStream() returs the following error.
ERROR c.d.f.s.util.FileAsyncProcessor - /private/var/tmp/upload_79f329ff_4cd2_46d0_b1a9_d0fac1ae27c2_00000020.tmp (No such file or directory)
java.io.FileNotFoundException: /private/var/tmp/upload_79f329ff_4cd2_46d0_b1a9_d0fac1ae27c2_00000020.tmp (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:194)
at org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:100)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getInputStream(StandardMultipartHttpServletRequest.java:250)
at com.de.files.service.util.FileAsyncProcessor.processFile(FileAsyncProcessor.java:58)
Upvotes: 0
Views: 3087
Reputation: 3512
You are having a scope problem.
The documentation of MultiPartFile says:
The file contents are either stored in memory or temporarily on disk. In either case, the user is responsible for copying file contents to a session-level or persistent store as and if desired. The temporary storage will be cleared at the end of request processing.
When you call your method and your method starts processing, the Request scope is gone. You should explicitly copy the file into an in-memory string or another temp location.
Upvotes: 2