解析Spring MVC上传文件

时间:2023-03-09 07:43:51
解析Spring MVC上传文件
  • 新建一个普通的maven工程
  • 在pom.xml文件中引入相应的坐标
     <?xml version="1.0" encoding="UTF-8"?>
    <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements. See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership. The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied. See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
    <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging> <name>springmvc_day02</name>
    <groupId>zh.test.springmvc</groupId>
    <artifactId>springmvc_day02</artifactId>
    <version>1.0-SNAPSHOT</version> <!-- 版本锁定 -->
    <properties>
    <spring.version>5.0.2.RELEASE</spring.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
    </dependency>
    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
    </dependency>
    <!--json字符串和Javabean对象互相转换的过程中,需要使用Jackson的jar包-->
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.0</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.0</version>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.0</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.4</version>
    </dependency>
    </dependencies>
    </project>
  • 编写一个文件上传的jsp页面
     <%--
    Created by IntelliJ IDEA.
    User: zhanghao
    Date: 2019-08-25
    Time: 17:05
    To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <h3>文件上传</h3>

    <form action="/fileupload.do" method="post" enctype="multipart/form-data">
    选择文件:<input type="file" name="upload" /><br/>
    <input type="submit" value="上传" />
    </form>
    </body>
    </html>
  • 在resource的目录下新建一个spring.xml的配置文件,用 1 <?xml version="1.0" encoding="UTF-8"?>
     <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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"> <!--如果<url-pattern>/</url-pattern>,任何资源都会拦截-->
    <!--配置哪些资源不被拦截-->
    <!--<mvc:resources mapping="/js/" location="/js/**" />-->
    <!--<mvc:resources mapping="/css/" location="/css/**" />-->
    <!--<mvc:resources mapping="/image/" location="/image/**" />--> <!--配置注解扫描-->
    <context:component-scan base-package="zh.test"/>
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--跳转的页面的路径-->
    <property name="prefix" value="/pages/" />
    <!--跳转页面的后缀名称-->
    <property name="suffix" value=".jsp" />
    </bean> <!--配置文件上传的解析器组件。id的名称是固定,不能乱写-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--设置上传文件的总大小 8M = 8 * 1024 * 1024 -->
    <property name="maxUploadSize" value="8388608" />
    </bean> </beans>
  • 编写一个controller
     package zh.test.demo2;
    
     import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest;
    import java.io.File;
    import java.io.IOException;
    import java.util.UUID; @Controller
    public class UploadController { @RequestMapping(path="/fileupload.do")
    public String upload(MultipartFile upload, HttpServletRequest request) throws IOException {
    // 把文件上传到哪个位置
    String realPath = request.getSession().getServletContext().getRealPath("/uploads");
    // 创建该文件夹
    File file = new File(realPath);
    // 判断该文件夹是否存在
    if(!file.exists()){
    // 创建文件夹
    file.mkdirs();
    } // 获取到上传文件的名称
    String filename = upload.getOriginalFilename(); // 把文件的名称修改成为一的值 sdfs-csdf-fwer-sdfw
    String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
    // 唯一的值
    filename = uuid+"_"+filename;
    System.out.println("文件名称:"+filename);
    // 上传文件
    upload.transferTo(new File(file,filename));
    return "suc";
    }
    }