跳转至

SpringMVC文件上传

1.基本介绍

  1. SpringMVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的。spring 用 Jacarta Commons FileUpload 技术实现了一个 MultipartResolver 的实现类:CommonsMultipartResovler

  2. SpringMVC 上下文默认没有装配 MultipartResolver ,因此默认情况下不能处理文件的上传工作。如果要使用 Spring 文件上传功能,要先在上下文(容器文件)中配置 MultipartResolver

<!--配置文件上传解析器-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" 
      id="multipartResolver">
</bean>

2.应用实例

(1)引入 SpringMVC 文件上传需要的 jar 包

image-20230217200556207

(2)创建 fileUpload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/fileUpload"
      method="post" enctype="multipart/form-data">
    文件介绍:<input type="text" name="introduce"/><br/>
    选择文件:<input type="file" name="file"/><br/>
    <input type="submit" value="上传文件"/>
</form>
</body>
</html>

(3)web.xml 文件中配置过滤器,处理中文乱码问题(这里使用 spring 提供的过滤器)

<!--使用spring提供的过滤器处理中文-->
<filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <!--大小写都支持-->
        <param-value>utf-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

(4)MyCharacterFilter.java

package com.li.web.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author 李
 * @version 1.0
 * 编写过滤器处理中文乱码问题
 */
public class MyCharacterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, 
                         ServletResponse servletResponse, 
                         FilterChain filterChain) throws IOException, ServletException {
        //加入对编码的处理
        servletRequest.setCharacterEncoding("utf-8");
        //放行请求
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {}
}

(5)在 spring 的容器文件中,配置文件上传解析器

<!--配置文件上传解析器-->
<!--这里是按照接口名字获取bean,因此 id要写为 multipartResolver(接口)-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
      id="multipartResolver">
</bean>

(6)创建 FileUploadHandler.java

package com.li.web.fileupload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @author 李
 * @version 1.0
 * 处理文件上传的 handler
 */
@Controller
public class FileUploadHandler {
    //编写方法,处理文件上传的请求
    @RequestMapping(value = "/fileUpload")
    public String fileUpload(@RequestParam(value = "file") MultipartFile multipartFile,
                             HttpServletRequest request, String introduce) 
        throws IOException {
        //接收到提交的文件名
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println("你上传的文件介绍=" + introduce);
        System.out.println("你上传的文件名=" + originalFilename);
        //得到上传的文件要保存的路径[全路径:包括文件名]
        //这里其实是out目录下的路径
        String fileFullPath =
                request.getServletContext().getRealPath("/img/" + originalFilename);
        //创建文件
        File saveToFile = new File(fileFullPath);
        //将上传文件转存到 saveToFile
        multipartFile.transferTo(saveToFile);
        return "success";
    }
}

(7)启动 tomcat,访问 fileUpload.jsp,选择文件上传

image-20230217204739917

后台输出:

image-20230217205540158

上传的文件:

这里只是模拟,真正开发中不会将文件存放在项目目录中,而是存放在主机的另外的磁盘上,每次存放文件时会将存放路径保存到数据库中。下载文件时会读取数据库数据,得到路径,寻找文件。

image-20230217204906903

postman 也可以进行文件上传:

image-20230217220956037

3.transferTO()方法

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
    //首先判断该文件(路径)在磁盘中是不是可获取的
   if (!isAvailable()) {
      throw new IllegalStateException("File has already been moved - cannot be transferred again");
   }

    //判断上传的文件在磁盘中存不存在,如果存在就会去尝试删除该文件,如果删除失败,就会抛出异常
   if (dest.exists() && !dest.delete()) {
      throw new IOException(
            "Destination file [" + dest.getAbsolutePath() + "] already exists and could not be deleted");
   }

    //如果文件不存在,或者存在但删除成功了
   try {
       //将上传的文件拷贝到目标目录 dest 下
       //this.fileItem 就是你要上传文件的临时文件(tmp文件)
       //就是说 SpringMVC 上传文件也是先将上传文件保存为临时文件,然后再拷贝为目标文件
      this.fileItem.write(dest);
      LogFormatUtils.traceDebug(logger, traceOn -> {
         String action = "transferred";
         if (!this.fileItem.isInMemory()) {
            action = (isAvailable() ? "copied" : "moved");
         }
         return "Part '" + getName() + "',  filename '" + getOriginalFilename() + "'" +
               (traceOn ? ", stored " + getStorageDescription() : "") +
               ": " + action + " to [" + dest.getAbsolutePath() + "]";
      });
   }
   catch (FileUploadException ex) {
      throw new IllegalStateException(ex.getMessage(), ex);
   }
   catch (IllegalStateException | IOException ex) {
      // Pass through IllegalStateException when coming from FileItem directly,
      // or propagate an exception from I/O operations within FileItem.write
      throw ex;
   }
   catch (Exception ex) {
      throw new IOException("File transfer failed", ex);
   }
}