Java SpringBoot

1.前端上传

A、Form表单上传

B、AJAX上传

2.后台保存

A、保存本地进行目录映射

①、在配置文件配置请求的目录和本地的映射目录-配置式便于更改,不用直接写死在文件中

application.yml
  1. uploadFile:
  2. # 请求 url 中的资源映射配置
  3. resourceHandler: /uploadFiles/**
  4. # 自定义上传文件本地保存路径
  5. location: D:\Competition\uploadFiles\

②、添加配置类实现WebMvcConfigurer接口

InterceptorConfig.java
  1. package com.fcant.competition.config;
  2. import com.fcant.competition.config.interceptor.AuthenticationInterceptor;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  7. import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  9. import org.springframework.web.servlet.resource.PathResourceResolver;
  10. import org.springframework.web.servlet.resource.WebJarsResourceResolver;
  11. /**
  12. * InterceptorConfig
  13. * <p>
  14. * encoding:UTF-8
  15. *
  16. * @author Fcant 下午 15:11:18 2020/4/6/0006
  17. */
  18. @Configuration
  19. public class InterceptorConfig implements WebMvcConfigurer {
  20. @Value("${uploadFile.resourceHandler}")
  21. private String resourceHandler;
  22. @Value("${uploadFile.location}")
  23. private String location;
  24. @Override
  25. public void addInterceptors(InterceptorRegistry registry) {
  26. registry.addInterceptor(authenticationInterceptor())
  27. .addPathPatterns("/**");
  28. }
  29. @Override
  30. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  31. registry.addResourceHandler("/static/**")
  32. .addResourceLocations("classpath:/static/");
  33. // 配置文件上传的本地映射路径
  34. registry.addResourceHandler(resourceHandler)
  35. .addResourceLocations("file:///" + location);
  36. /**
  37. * 注册静态资源webjar的映射路径
  38. */
  39. registry.addResourceHandler("/webjars/**")
  40. .addResourceLocations("classpath:/META-INF/resources/webjars/")
  41. .resourceChain(false)
  42. .addResolver(new WebJarsResourceResolver())
  43. .addResolver(new PathResourceResolver());
  44. }
  45. }

③、文件保存处理的接口

FileUploadController.java
  1. package com.fcant.competition.api.v1;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.io.File;
  9. import java.io.IOException;
  10. /**
  11. * FileUploadController
  12. * <p>
  13. * encoding:UTF-8
  14. *
  15. * @author Fcant 上午 09:35:01 2020/4/10/0010
  16. */
  17. @Controller
  18. @ResponseBody
  19. @RequestMapping("/file")
  20. public class FileController {
  21. @Value("${uploadFile.location}")
  22. private String location;
  23. @RequestMapping("/upload")
  24. public void saveFile(MultipartFile uploadFile) {
  25. File file = new File(location, uploadFile.getOriginalFilename());
  26. File dir = new File(location);
  27. if (!dir.exists()) {
  28. // 使用mkdirs创建多级目录
  29. dir.mkdirs();
  30. }
  31. try {
  32. uploadFile.transferTo(file);
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. }

B、保存至class文件的编译目录(不推荐:不利于打包部署至服务器)

3.配置大文件上传

SpringBoot默认每个文件的配置最大为1Mb,单次请求的文件的总数不能大于10Mb。
image.png
**multipart.maxFileSize=10Mb**是设置单个文件的大小, **multipart.maxRequestSize=100Mb**是设置单次请求的文件的总大小,如果是想要不限制文件上传的大小,那么就把两个值都设置为-1就可以了。
SpringBoot1.4之前的版本配置参数

  1. multipart.maxFileSize=10Mb
  2. multipart.maxRequestSize=100Mb

Spring Boot1.4版本后配置更改为:

  1. spring.http.multipart.maxFileSize=10Mb
  2. spring.http.multipart.maxRequestSize=100Mb

Spring Boot2.0之后的版本配置修改为:

  1. spring.servlet.multipart.max-file-size=10MB
  2. spring.servlet.multipart.max-request-size=100MB