1.前端上传
A、Form表单上传
B、AJAX上传
2.后台保存
A、保存本地进行目录映射
①、在配置文件配置请求的目录和本地的映射目录-配置式便于更改,不用直接写死在文件中
application.yml
uploadFile:
# 请求 url 中的资源映射配置
resourceHandler: /uploadFiles/**
# 自定义上传文件本地保存路径
location: D:\Competition\uploadFiles\
②、添加配置类实现WebMvcConfigurer接口
InterceptorConfig.java
package com.fcant.competition.config;
import com.fcant.competition.config.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.WebJarsResourceResolver;
/**
* InterceptorConfig
* <p>
* encoding:UTF-8
*
* @author Fcant 下午 15:11:18 2020/4/6/0006
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Value("${uploadFile.resourceHandler}")
private String resourceHandler;
@Value("${uploadFile.location}")
private String location;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authenticationInterceptor())
.addPathPatterns("/**");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("classpath:/static/");
// 配置文件上传的本地映射路径
registry.addResourceHandler(resourceHandler)
.addResourceLocations("file:///" + location);
/**
* 注册静态资源webjar的映射路径
*/
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.resourceChain(false)
.addResolver(new WebJarsResourceResolver())
.addResolver(new PathResourceResolver());
}
}
③、文件保存处理的接口
FileUploadController.java
package com.fcant.competition.api.v1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
/**
* FileUploadController
* <p>
* encoding:UTF-8
*
* @author Fcant 上午 09:35:01 2020/4/10/0010
*/
@Controller
@ResponseBody
@RequestMapping("/file")
public class FileController {
@Value("${uploadFile.location}")
private String location;
@RequestMapping("/upload")
public void saveFile(MultipartFile uploadFile) {
File file = new File(location, uploadFile.getOriginalFilename());
File dir = new File(location);
if (!dir.exists()) {
// 使用mkdirs创建多级目录
dir.mkdirs();
}
try {
uploadFile.transferTo(file);
} catch (IOException e) {
e.printStackTrace();
}
}
}
B、保存至class文件的编译目录(不推荐:不利于打包部署至服务器)
3.配置大文件上传
SpringBoot默认每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。**multipart.maxFileSize=10Mb**
是设置单个文件的大小, **multipart.maxRequestSize=100Mb**
是设置单次请求的文件的总大小,如果是想要不限制文件上传的大小,那么就把两个值都设置为-1就可以了。
SpringBoot1.4之前的版本配置参数
multipart.maxFileSize=10Mb
multipart.maxRequestSize=100Mb
Spring Boot1.4版本后配置更改为:
spring.http.multipart.maxFileSize=10Mb
spring.http.multipart.maxRequestSize=100Mb
Spring Boot2.0之后的版本配置修改为:
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB