0

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)
4
  • 2
    The file should be in contained within the root of your project. Commented Mar 19, 2019 at 5:08
  • Check the path of your file, file.getInputStream() try to get file in "/private/var/tmp/" I think it should be the root like "/var/tmp/" without "private" Commented Mar 19, 2019 at 5:16
  • I see this issue only for tomcat server. When I used Jetty, I could not reproduce this. Commented Mar 27, 2019 at 3:31
  • <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> Commented Mar 27, 2019 at 3:31

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.