问题

上传Excel进行处理 不用用户等待 处理完之后 会邮件告知处理结果
所以是异步处理

public void readExcel(@RequestParam("file") MultipartFile file) {
        
    CompletableFuture.runAsync(() -> {
        try {
            service.readExcel(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}

实际使用的过程中 发现经常上传失败 报错如下

java.io.FileNotFoundException: /private/tmp/upload_2cc08ec8_c3eb_4633_8c0f_0cc20e34ff55_00000000.tmp (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at org.apache.tomcat.util.http.fileupload.disk.DiskFileItem.getInputStream(DiskFileItem.java:188)
    at org.apache.catalina.core.ApplicationPart.getInputStream(ApplicationPart.java:100)
    at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.getInputStream(StandardMultipartHttpServletRequest.java:297)

原因

请求返回后 会自动删除临时文件 导致实际读取Excel的时候 找不到文件

// org.apache.catalina.core.ApplicationPart#delete
public void delete() throws IOException {
    fileItem.delete();
}

解决

复制该临时文件 将复制后的文件传给异步程序处理 处理完之后删除该临时文件

String uuid = UUID.randomUUID().toString().replace('-', '_');
String tmpFileName = String.format("/tmp/upload_%s.tmp", uuid);
final File tmpFile = new File(tmpFileName);
try {
    Files.copy(file.getInputStream(), tmpFile.toPath());
} catch (IOException e) {
    e.printStackTrace();
}
CompletableFuture.runAsync(() -> {
    try {
        service.readExcel(tmpFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
});

参考文档

https://stackoverflow.com/que...


zhuguowei2
825 声望26 粉丝

引用和评论

0 条评论