共计 1418 个字符,预计需要花费 4 分钟才能阅读完成。
问题
上传 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…
正文完
发表至: spring-mvc
2020-12-19