When uploading files, use multipartfile Transferto() saves the file to the local path:
report errors:
java.io.IOException: java.io.FileNotFoundException: C:\Users\XXXXX\AppData\Local\Temp\tomcat. 8350081478984499756.8080\work\Tomcat\localhost\ROOT\app\file\xxxx. Xlsx (the system cannot find the specified path.)
@Override
public String store(MultipartFile file, String fileName) throws IOException {
String destPath "/app/file/";
File filePath = new File(destPath);
File dest = new File(filePath, fileName);
if (!filePath.exists()) {
filePath.mkdirs();
}
try {
file.transferTo(dest);
log.info("file save success");
} catch (IOException e) {
log.error("File upload Error: ", e);
throw e;
}
return dest.getCanonicalPath();
}
Cause analysis:
file. When the transferto method is called, it is judged that if it is a relative path, the temp directory is used as the parent directory
so it is saved in the temporary work directory of Tomcat.
Solution:
Use absolute path: filepath.getAbsolutePath()
@Override
public String store(MultipartFile file, String fileName) throws IOException {
String destPath "/app/file/";
File filePath = new File(destPath);
// Convert to absolute path
File dest = new File(filePath.getAbsolutePath(), fileName);
if (!filePath.exists()) {
filePath.mkdirs();
}
try {
file.transferTo(dest);
log.info("file save success");
} catch (IOException e) {
log.error("File upload Error: ", e);
throw e;
}
return dest.getCanonicalPath();
}
Supplement:
You can also file Getbytes() gets the byte array, or file Getinputstream() performs stream data operation and writes it to disk.
Access to uploaded files
spring:
resources:
static-locations: file:/app/file/ #Access external system resources and map the files in this directory to the system
or
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String absolutePath = new File("/app/file/").getAbsolutePath();
registry.addResourceHandler("/upload/**") // External Access Addresses
.addResourceLocations("file:" + absolutePath)// SpringBoot needs to add the file protocol prefix
.setCacheControl(CacheControl.maxAge(30, TimeUnit.MINUTES));// set the browser cache
}
}
Read More:
- After asynchronous file import and springboot multipartfile upload, the @async asynchronous processing reports an error: nosuchfileexception
- JAVA: How to Use Multipartfile to upload Files
- SpringCloud Use openFeign Multipartfile to Upload Files Error: Current request is not a multipart request
- Multipartfiletofileutils (multipartfile to file)
- Upload file error analysis standardmultiparthttpservletrequest
- [Solved] Read the resources resource and convert it to file error: java.io.filenotfoundexception
- Springboot Files Upload Limit Error: The field file exceeds its maximum permitted size of 1048576 bytes
- [Solved] Pycharm Failed to Upload: Upload to *** failed. Could not list the contents of folder “sftp
- [Solved] Springboot upload failed to find the temporary directory error
- [Solved] Failed to bind properties under ‘spring.servlet.multipart.file-size-threshold‘ to
- Doris streamload task reported an error connection reset [How to Solve]
- JAVA: How to Use Minio to upload pictures
- [Solved] Upload Files Error: Request processing failed;nested exception is org.springframework.web.multipart.MultipartExcepti
- Tomcat cross server upload error 403forbidden [How to Solve]
- [Solved] Subversion reported an error:The repository at ‘‘has uuid
- How to Solve Springboot Upload Files Error: The field XXX exceeds its maximum permitted size of 1048576 bytes
- [How to Fix]Spring 3.0 could not find acceptable representation
- [Solved] spring boot Startup Error: Error creating bean with name ‘requestMappingHandlerMapping‘ defined in class path
- Spring MVC uses Ajax to submit requests asynchronously to complete login
- How to Solve Image Upload Error: Uncaught (in promise) DOMException: Failed to execute ‘put‘ on ‘IDBObjectStore‘