欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringMVC 单个或多个文件上传

程序员文章站 2022-05-04 19:53:29
1. 添加依赖 org.springframework spring-webmvc 5.2.10.RELEASE <...

1. 添加依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>

2. 编写SpringMVC核心配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <context:component-scan base-package="site.zhouyun.controller" />

        <mvc:annotation-driven />

        <!-- 视图解析器 -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>

        <!-- 文件上传解析器 -->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!-- 最大上传大小, 单位byte -->
            <property name="maxUploadSize" value="5242880" ></property>
            <!-- 默认编码 -->
            <property name="defaultEncoding" value="UTF-8"></property>
        </bean>
</beans>

3. 修改web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <!-- dispatcherServlet -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4. 编写Controller

@Controller
@RequestMapping("upload")
public class FileUploadController
{
    /**
     * 单文件上传
     * @param uploadFile
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("file")
    public String fileUpload(MultipartFile uploadFile) throws IOException
    {
        System.out.println("uploadFile: " + uploadFile);
        if(uploadFile != null)
        {
            // 获取文件名
            String filename = uploadFile.getOriginalFilename();
            System.out.println("filename: " + filename);
            // 保存路径
            uploadFile.transferTo(new File("/home/cloud/temp/" + filename));
            return "upload success!";
        }
        return "upload error!";
    }

    /**
     * 多文件上传
     * @param uploadFile
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("files")
    public String filesUpload(MultipartFile[] uploadFile) throws IOException
    {
        for (MultipartFile multipartFile : uploadFile)
        {
            if(uploadFile != null)
            {
                String filename = multipartFile.getOriginalFilename();
                System.out.println("filename: " + filename);
                multipartFile.transferTo(new File("/home/cloud/temp/" + filename));
            }
            else
            {
                return "upload error!";
            }
        }
        return "upload success!";
    }
}

5. 编写jsp

upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <!-- 单文件上传 -->
    <form action="${pageContext.request.contextPath}/upload/file" method="post" enctype="multipart/form-data">
        文件1: <input type="file" name="uploadFile"><br/><br/>
              <input type="submit" value="上传">
    </form>
</body>
</html>

uploads.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <!-- 多文件上传 -->
    <form action="${pageContext.request.contextPath}/upload/files" method="post" enctype="multipart/form-data">
        文件1: <input type="file" name="uploadFile"><br/><br/>
        文件2: <input type="file" name="uploadFile"><br/><br/>
        文件3: <input type="file" name="uploadFile"><br/><br/>
        <input type="submit" value="上传">
    </form>
</body>
</html>

6. 查看效果

  • 启动项目打开浏览器访问http://localhost:8080/upload.jsp 上传单个文件查看效果.
  • 启动项目打开浏览器访问http://localhost:8080/uploads.jsp 上传多个文件查看效果.

本文地址:https://blog.csdn.net/ZY4444444/article/details/110183712