First question
When there is a large amount of data in Excel, the process of Java background parsing may take a long time, but users do not need to wait. At this time, consider the asynchronous import of files
There are several ways to implement the file asynchronous method. Here, it is implemented by specifying the asynchronous thread pool, that is, @ async (“thread pool name”) annotates the asynchronous method.
However, after testing, it is found that the annotations of this annotation are also annotated, but the asynchronous effect cannot be realized.
After several twists and turns, it is found that asynchronous methods can call non asynchronous methods, which can achieve asynchronous effect; First, non asynchronous methods call asynchronous methods, which will fail. What is said here is in the same Java class.
In different Java classes, the above problems do not exist
The second problem
uses the asynchronous method to receive files through the controller and process them in the service layer. At this time, the controller has returned the results of successful execution. The rest is to analyze and store them in the service layer. Unexpectedly, an error is reported in the service
java.nio.file.NoSuchFileException: D:\UserData\Temp\undertow.1407321862395783323.8400\undertow4517937229384702645upload
Methods in controller
@PostMapping("/test")
public R<String> test(MultipartFile file) {
testService.test(file);
return R.success("successful to import");
}
Methods in service
@Async("asyncImportExecutor")
public void test(MultipartFile file) {
try {
EasyExcelUtil.read(file.getInputStream(), Test.class, this::executeImport)
.sheet().doRead();
} catch (Exception ex) {
log.error("[test]Asynchronous import exceptions:", ex);
}
}
When I saw this exception nosuchfileexception, I was stunned. The test was conducted through postman, so I began to suspect the problem of postman. After troubleshooting, there was no problem with postman and path, and the asynchronous call process was OK
Then think of the abnormal prompt, that is, the file can not be found. According to the printed log, it can’t be found locally. Then there was the following writing
Methods in controller
@PostMapping("/test")
public R<String> test(MultipartFile file) {
try {
testService.test(file.getInputStream());
} catch (IOException e) {
log.error("[test]Exception log:", e);
return R.fail("Import failed");
}
return R.success("Import successful");
}
Methods in service
@Async("asyncImportExecutor")
public void test(InputStream file) {
try {
EasyExcelUtil.read(file, Test.class, this::executeImport)
.sheet().doRead();
} catch (Exception ex) {
log.error("[test]Asynchronous import exceptions:", ex);
}
}
So you don’t report mistakes
later, I debugged and found that the temporary file was generated by the multipartfile object in the controller layer. I had been writing the synchronization method, and I didn’t notice that the multipartfile object would generate a temporary file. Later, it was found that after the result returned in the controller, there was no temporary file.
Error summary:
because the asynchronous method is used, there will be a main thread and an asynchronous thread. After uploading a file, an instance of multipartfile type will be formed and a temporary file will be generated. At this time, it is in the main thread. After the instance of multipartfile is handed over to the asynchronous thread for processing, the temporary file will be destroyed by springboot (spring). The above exception will appear when you go to getinputstream in the asynchronous thread.
The following method is to take the InputStream stream as the input parameter, so the file cannot be found.
Read More:
- Spring boot uses thread pool to realize asynchronous processing without return and asynchronous processing with return
- Asynchronous processing of HTTP request by Java_ Method 2: through deferredresult
- Asynchronous processing of HTTP request by Java_ Method 1: through callable
- [Solved] Spring upload file Error: Multipartfile Transferto() reported an error FileNotFoundException
- [Solved] Jenkins error: Asynchronous execution failure
- JAVA: How to Use Multipartfile to upload Files
- Springboot reports an error After adding the import annotation: application failed to start
- Asynchronous callback case of Java callback function
- Multipartfiletofileutils (multipartfile to file)
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- Springboot Files Upload Limit Error: The field file exceeds its maximum permitted size of 1048576 bytes
- [Solved] Springboot upload failed to find the temporary directory error
- [Solved] Upload Files Error: Request processing failed;nested exception is org.springframework.web.multipart.MultipartExcepti
- How to Solve Springboot Upload Files Error: The field XXX exceeds its maximum permitted size of 1048576 bytes
- Upload file error analysis standardmultiparthttpservletrequest
- Springboot reports an error whitelabel error page
- Java will convert Excel to list set or JSON, export excel file to local, excel import and export, easyexcel tool class
- Mybatis reports an error (error building sqlsession.) when using annotations without deleting redundant files
- [Solved] Solr8 establishes the cluster node as active, but the query reports an error
- JAVA: How to Use Minio to upload pictures