解析Spring MVC上传文件

  • 新建一个普通的maven工程
  • 在pom.xml文件中引入相应的坐标
     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!--
     3   Licensed to the Apache Software Foundation (ASF) under one
     4   or more contributor license agreements.  See the NOTICE file
     5   distributed with this work for additional information
     6   regarding copyright ownership.  The ASF licenses this file
     7   to you under the Apache License, Version 2.0 (the
     8   "License"); you may not use this file except in compliance
     9   with the License.  You may obtain a copy of the License at
    10 
    11    http://www.apache.org/licenses/LICENSE-2.0
    12 
    13   Unless required by applicable law or agreed to in writing,
    14   software distributed under the License is distributed on an
    15   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    16   KIND, either express or implied.  See the License for the
    17   specific language governing permissions and limitations
    18   under the License.
    19 -->
    20 <!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
    21 <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">
    22 
    23   <modelVersion>4.0.0</modelVersion>
    24   <packaging>war</packaging>
    25 
    26   <name>springmvc_day02</name>
    27   <groupId>zh.test.springmvc</groupId>
    28   <artifactId>springmvc_day02</artifactId>
    29   <version>1.0-SNAPSHOT</version>
    30 
    31   <!-- 版本锁定 -->
    32   <properties>
    33     <spring.version>5.0.2.RELEASE</spring.version>
    34   </properties>
    35   <dependencies>
    36     <dependency>
    37       <groupId>org.springframework</groupId>
    38       <artifactId>spring-context</artifactId>
    39       <version>${spring.version}</version>
    40     </dependency>
    41     <dependency>
    42       <groupId>org.springframework</groupId>
    43       <artifactId>spring-web</artifactId>
    44       <version>${spring.version}</version>
    45     </dependency>
    46     <dependency>
    47       <groupId>org.springframework</groupId>
    48       <artifactId>spring-webmvc</artifactId>
    49       <version>${spring.version}</version>
    50     </dependency>
    51     <dependency>
    52       <groupId>javax.servlet</groupId>
    53       <artifactId>servlet-api</artifactId>
    54       <version>2.5</version>
    55       <scope>provided</scope>
    56     </dependency>
    57     <dependency>
    58       <groupId>javax.servlet.jsp</groupId>
    59       <artifactId>jsp-api</artifactId>
    60       <version>2.0</version>
    61       <scope>provided</scope>
    62     </dependency>
    63     <!--json字符串和Javabean对象互相转换的过程中,需要使用Jackson的jar包-->
    64     <dependency>
    65       <groupId>com.fasterxml.jackson.core</groupId>
    66       <artifactId>jackson-databind</artifactId>
    67       <version>2.9.0</version>
    68     </dependency>
    69     <dependency>
    70       <groupId>com.fasterxml.jackson.core</groupId>
    71       <artifactId>jackson-core</artifactId>
    72       <version>2.9.0</version>
    73     </dependency>
    74     <dependency>
    75       <groupId>com.fasterxml.jackson.core</groupId>
    76       <artifactId>jackson-annotations</artifactId>
    77       <version>2.9.0</version>
    78     </dependency>
    79     <!--文件上传-->
    80     <dependency>
    81       <groupId>commons-fileupload</groupId>
    82       <artifactId>commons-fileupload</artifactId>
    83       <version>1.3.1</version>
    84     </dependency>
    85     <dependency>
    86       <groupId>commons-io</groupId>
    87       <artifactId>commons-io</artifactId>
    88       <version>2.4</version>
    89     </dependency>
    90   </dependencies>
    91 </project>

     

  • 编写一个文件上传的jsp页面
     1 <%--
     2   Created by IntelliJ IDEA.
     3   User: zhanghao
     4   Date: 2019-08-25
     5   Time: 17:05
     6   To change this template use File | Settings | File Templates.
     7 --%>
     8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     9 <html>
    10 <head>
    11     <title>Title</title>
    12 </head>
    13 <body>
    14 <h3>文件上传</h3>
    15 16 <form action="/fileupload.do" method="post" enctype="multipart/form-data">
    17     选择文件:<input type="file" name="upload" /><br/>
    18     <input type="submit" value="上传" />
    19 </form>
    20 </body>
    21 </html>

     

  • 在resource的目录下新建一个spring.xml的配置文件,用 1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:mvc="http://www.springframework.org/schema/mvc"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     6        xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans
     8         http://www.springframework.org/schema/beans/spring-beans.xsd
     9         http://www.springframework.org/schema/mvc
    10         http://www.springframework.org/schema/mvc/spring-mvc.xsd
    11         http://www.springframework.org/schema/context
    12         http://www.springframework.org/schema/context/spring-context.xsd">
    13 
    14     <!--如果<url-pattern>/</url-pattern>,任何资源都会拦截-->
    15     <!--配置哪些资源不被拦截-->
    16     <!--<mvc:resources mapping="/js/" location="/js/**" />-->
    17     <!--<mvc:resources mapping="/css/" location="/css/**" />-->
    18     <!--<mvc:resources mapping="/image/" location="/image/**" />-->
    19 
    20     <!--配置注解扫描-->
    21     <context:component-scan base-package="zh.test"/>
    22     <!--配置视图解析器-->
    23     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    24         <!--跳转的页面的路径-->
    25         <property name="prefix" value="/pages/" />
    26         <!--跳转页面的后缀名称-->
    27         <property name="suffix" value=".jsp" />
    28     </bean>
    29 
    30     <!--配置文件上传的解析器组件。id的名称是固定,不能乱写-->
    31     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    32         <!--设置上传文件的总大小 8M = 8 * 1024 * 1024 -->
    33         <property name="maxUploadSize" value="8388608" />
    34     </bean>
    35     
    57 </beans>

     

  • 编写一个controller
     1 package zh.test.demo2;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.multipart.MultipartFile;
     6 
     7 import javax.servlet.http.HttpServletRequest;
     8 import java.io.File;
     9 import java.io.IOException;
    10 import java.util.UUID;
    11 
    12 @Controller
    13 public class UploadController {
    14 
    15     @RequestMapping(path="/fileupload.do")
    16     public String upload(MultipartFile upload, HttpServletRequest request) throws IOException {
    17         // 把文件上传到哪个位置
    18         String realPath = request.getSession().getServletContext().getRealPath("/uploads");
    19         // 创建该文件夹
    20         File file = new File(realPath);
    21         // 判断该文件夹是否存在
    22         if(!file.exists()){
    23             // 创建文件夹
    24             file.mkdirs();
    25         }
    26 
    27         // 获取到上传文件的名称
    28         String filename = upload.getOriginalFilename();
    29 
    30         // 把文件的名称修改成为一的值 sdfs-csdf-fwer-sdfw
    31         String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
    32         // 唯一的值
    33         filename = uuid+"_"+filename;
    34         System.out.println("文件名称:"+filename);
    35         // 上传文件
    36         upload.transferTo(new File(file,filename));
    37         return "suc";
    38     }
    39 }

     

posted @ 2019-11-23 16:17  白天黑天阴天  阅读(201)  评论(0编辑  收藏  举报