• SpringMVC整合Servlet3文件上传


    1、配置web.xml文件

    注意:web的版本必须大于2.5

    1.  
    2.  
    3.  <multipart-config>
    4.      
    5.      <max-file-size>5242880max-file-size>
    6.      <max-request-size>20971520max-request-size>
    7.      <file-size-threshold>0file-size-threshold>
    8.  multipart-config>

    2、配置springmvc.xml文件

    1.  
    2.  <bean id="multipartResolver" class="org.springframework.web.multipart.support.StandardServletMultipartResolver"/>

    3、前端

    1.  <form action="${request.contextPath}/brand/add" method="post" enctype="multipart/form-data">
    2.     Logo:<input type="file" name="logoFile"><br>
    3.      <input type="submit" value="提交">
    4.  form>

    4.1、后端上传到本地文件夹

    1.  import com.xxx.constant.WegoConst;
    2.  import org.springframework.stereotype.Controller;
    3.  import org.springframework.web.bind.annotation.PostMapping;
    4.  import org.springframework.web.bind.annotation.RequestMapping;
    5.  ​
    6.  import javax.servlet.ServletException;
    7.  import javax.servlet.annotation.MultipartConfig;
    8.  import javax.servlet.http.HttpServletRequest;
    9.  import javax.servlet.http.Part;
    10.  import java.io.IOException;
    11.  import java.util.UUID;
    12.  ​
    13.  /**
    14.   * @author JngKang
    15.   * @date 2022-05-16 09:27
    16.   */
    17.  @Controller
    18.  @MultipartConfig
    19.  @RequestMapping("brand")
    20.  public class BrandController {
    21.  ​
    22.      @PostMapping("add")
    23.      public void add(HttpServletRequest request) throws ServletException, IOException {
    24.          final Part part = request.getPart("logoFile");
    25.          // 获取请求头
    26.          final String header = part.getHeader("content-disposition");
    27.          // 获取文件名
    28.          final String fileName = getFileName(header);
    29.          final String suffix = fileName.substring(fileName.lastIndexOf("."));
    30.          final String newFileName = UUID.randomUUID() + suffix;
    31.          final String filePath = WegoConst.FILEPATH + newFileName;
    32.          try {
    33.              part.write(filePath);
    34.              System.out.println(fileUrl);
    35.         } catch (IOException e) {
    36.              throw new RuntimeException(e);
    37.         }
    38.     }
    39.  ​
    40.      /**
    41.       * 根据请求头解析出文件名
    42.       *
    43.       * @param header 请求头
    44.       * @return 文件名
    45.       */
    46.      public String getFileName(String header) {
    47.          String[] temp = header.split(";")[2].split("=");
    48.          //获取文件名,兼容各种浏览器的写法
    49.          String fileName = temp[1].substring(temp[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
    50.          return fileName;
    51.     }
    52.  }

    4.2、后端上传至Nginx图片服务器

     
    1. import com.xxx.constant.WegoConst;
    2.  import com.xxx.domain.Brand;
    3.  import com.xxx.service.BrandService;
    4.  import org.springframework.stereotype.Controller;
    5.  import org.springframework.web.bind.annotation.PostMapping;
    6.  import org.springframework.web.bind.annotation.RequestMapping;
    7.  ​
    8.  import javax.annotation.Resource;
    9.  import javax.servlet.ServletException;
    10.  import javax.servlet.annotation.MultipartConfig;
    11.  import javax.servlet.http.HttpServletRequest;
    12.  import javax.servlet.http.Part;
    13.  import java.io.IOException;
    14.  import java.util.UUID;
    15.  ​
    16.  /**
    17.   * @author JngKang
    18.   * @date 2022-05-16 09:27
    19.   */
    20.  @Controller
    21.  @MultipartConfig
    22.  @RequestMapping("brand")
    23.  public class BrandController {
    24.  ​
    25.      @Resource
    26.      private BrandService brandService;
    27.  ​
    28.      @PostMapping("add")
    29.      public String add(HttpServletRequest request, Brand brand) throws ServletException, IOException {
    30.          final Part part = request.getPart("logoFile");
    31.          // 获取请求头
    32.          final String header = part.getHeader("content-disposition");
    33.          // 获取文件名
    34.          final String fileName = getFileName(header);
    35.          final String suffix = fileName.substring(fileName.lastIndexOf("."));
    36.          final String newFileName = UUID.randomUUID() + suffix;
    37.          final String filePath = WegoConst.FILEPATH + newFileName;
    38.          final String fileUrl = "http://localhost:8082/" + newFileName;
    39.          try {
    40.              part.write(filePath);
    41.              System.out.println(fileUrl);
    42.         } catch (IOException e) {
    43.              throw new RuntimeException(e);
    44.         }
    45.          brand.setLogo(fileUrl);
    46.          int insert = brandService.insert(brand);
    47.          if (insert == 1) {
    48.              return "redirect:/brand/list";
    49.         }
    50.          return "redirect:/brand/openAddPage";
    51.     }
    52.  ​
    53.      /**
    54.       * 根据请求头解析出文件名
    55.       *
    56.       * @param header 请求头
    57.       * @return 文件名
    58.       */
    59.      public String getFileName(String header) {
    60.          String[] temp = header.split(";")[2].split("=");
    61.          //获取文件名,兼容各种浏览器的写法
    62.          String fileName = temp[1].substring(temp[1].lastIndexOf("\\") + 1).replaceAll("\"", "");
    63.          return fileName;
    64.     }
    65.  }

  • 相关阅读:
    Vue的学习笔记四:组件
    科技与狠活儿丨Vue项目中Pinia状态管理工具的使用
    Spring Boot项目的搭建和运行
    FastDFS简介及安装部署(CentOS7)
    七甲川荧光染料标记酸,Cy7 酸,Cy7-acid,CAS 943298-08-6参数及特性解析
    [Linux]进程间通信--管道
    Radius 成为云原生计算基金会(CNCF)的沙箱项目
    01_Elasticsearch入门介绍
    postgresql-窗口函数种类
    聊聊开关和CPU之间故事
  • 原文地址:https://blog.csdn.net/JngKang/article/details/127611620