springboot中多线程中使用MultipartFile进行异步操作报错,系统找不到指定的文件

springboot中多线程中使用MultipartFile进行异步操作报错,系统找不到指定的文件

首先,前端传递的文件会存储在一个临时文件夹中,也就是这样的路径。[en]First of all, the files passed by the front end will be stored in a temporary folder, that is, a path like this.

<span class="hljs-selector-tag">C</span>:\<span class="hljs-selector-tag">Users</span>\<span class="hljs-selector-tag">xxx</span>\<span class="hljs-selector-tag">AppData</span>\<span class="hljs-selector-tag">Local</span>\<span class="hljs-selector-tag">Temp</span>\<span class="hljs-selector-tag">tomcat</span><span class="hljs-selector-class">.6131519677783180826</span><span class="hljs-selector-class">.8056</span>\<span class="hljs-selector-tag">work</span>\<span class="hljs-selector-tag">Tomcat</span>\<span class="hljs-selector-tag">localhost</span>\<span class="hljs-selector-tag">ROOT</span>

但在异步执行时,主线程结束,临时文件被清空,因此会报告错误:[en]But when executing asynchronously, the main thread ends and the temporary file is emptied, so an error is reported:

java.io.FileNotFoundException: C:\Users\hwq\AppData\Local\Temp\tomcat.6131519677783180826.8056\work\Tomcat\localhost\ROOT\upload_85d787c3_6037_4ea2_a7f9_54ac3a19b461_00000011.tmp (系统找不到指定的文件。)

需要转换为流才能运行[en]Need to be converted to a stream to operate

 @RequestMapping("/add")    public String addRecordDetail(@RequestParam("file") MultipartFile file, @ModelAttribute FormDetail formDetail) throws IOException {            String fileName = buildFileName(file);            InputStream inputStream = file.getInputStream();            RecordDetailThreadMange detailThreadMange = new RecordDetailThreadMange(inputStream,formDetail,fileName);            threadPoolService.addThreadPoolTask(detailThreadMange);            logger.debug("============>run next process,当前线程名称:{}",Thread.currentThread().getName());            return "true";    }

原文地址:https://blog.csdn.net/javadream007/article/details/102566585

Original: https://www.cnblogs.com/jpfss/p/12193163.html
Author: 星朝
Title: springboot中多线程中使用MultipartFile进行异步操作报错,系统找不到指定的文件

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/6135/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总