gpt4 book ai didi

java - spring boot 和 tomcat - 找不到文件

转载 作者:行者123 更新时间:2023-12-02 10:07:33 24 4
gpt4 key购买 nike

我尝试上传文件,我的服务调用 spring @Async 方法并传递文件对象。

fileAsyncProcessor.process(file);

此后,文件对象在 process 方法中变为 null

    public class FileAsyncProcessor {             
@Async
public void process(MultipartFile file) {
log.debug("processing file...");
InputStream is = file.getInputStream();
//.....
}
}

file.getInputStream() 返回以下错误。

    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)

最佳答案

您遇到范围问题。

The documentation of MultiPartFile说:

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.

当您调用您的方法并且您的方法开始处理时,请求范围就消失了。您应该显式地将文件复制到内存中的字符串或另一个临时位置。

关于java - spring boot 和 tomcat - 找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55233970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com