springMVC的文件上传

一、工程目录结构

  

二、web.xml

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 5          version="4.0">
 6 
 7 
 8     <!--字符编码过滤器CharacterEncodingFilter,防止提交表单乱码
 9         放在最前面,可以对所有的起作用-->
10 
11     <filter>
12         <filter-name>encodingFilter</filter-name>
13         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
14             <init-param>
15                 <param-name>encoding</param-name>
16                 <param-value>utf-8</param-value>
17             </init-param>
18     </filter>
19 
20     <filter-mapping>
21         <filter-name>encodingFilter</filter-name>
22         <url-pattern>/*</url-pattern>
23     </filter-mapping>
24     
25     
26     <!--前端控制器-->
27 
28     <servlet>
29         <servlet-name>dispatcherServlet</servlet-name>
30         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
31             <init-param>
32                 <param-name>contextConfigLocation</param-name>
33                 <param-value>classpath:spring/springMVC.xml</param-value>
34             </init-param>
35     </servlet>
36     <servlet-mapping>
37         <servlet-name>dispatcherServlet</servlet-name>
38         <url-pattern>/</url-pattern>
39     </servlet-mapping>
40     
41 </web-app>

三、springMVC.xml

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 6 
 7     <!--包扫描器-->
 8     <context:component-scan base-package="com.wang.mvc.controller"/>
 9     
10     <!--内部资源视图解析器-->
11     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
12           p:prefix="/jsp/" p:suffix=".jsp"/>
13     
14     <!-- Commons多文件上传处理器
15          注意:一定要写id="multipartResolver",否则报错-->
16 
17     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
18         <property name="maxUploadSize" value="1024000000"/>
19     </bean>
20     
21     
22     
23 </beans>

  

四、前端页面

  1.login.jsp

  

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: 0
 4   Date: 2019/1/3
 5   Time: 16:10
 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>Login</title>
12 </head>
13 <body>
14     Login page...
15     <br>
16     ------单文件上传-------
17     <br>
18     <form action="uploadFile" method="post" enctype="multipart/form-data">
19         username:<input type="text" name="username"/>
20         <br>
21         file:<input type="file" name="uploadFile" />
22         <br>
23         <input type="submit" value="上传"/>
24     </form>
25 
26     <br>
27     ------多文件上传-------
28     <br>
29     <form action="uploadMoreFile" method="post" enctype="multipart/form-data">
30         username:<input type="text" name="username"/>
31         <br>
32         file1:<input type="file" name="uploadFile" />
33         <br>
34         file2:<input type="file" name="uploadFile" />
35         <br>
36         file3:<input type="file" name="uploadFile" />
37         <br>
38         <input type="submit" value="上传"/>
39     </form>
40 </body>
41 </html>

       2.success.jsp

<%--
  Created by IntelliJ IDEA.
  User: 0
  Date: 2019/1/3
  Time: 16:40
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Success</title>
</head>
<body>
    Success page...
    <br>
    文件上传成功!


</body>
</html>

五、UploadFile.java

 1 package com.wang.mvc.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestMethod;
 6 import org.springframework.web.bind.annotation.RequestParam;
 7 import org.springframework.web.multipart.MultipartFile;
 8 
 9 import javax.servlet.http.HttpServletRequest;
10 import java.io.File;
11 import java.io.IOException;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14 
15 @Controller
16 public class UploadFile {
17 
18     @RequestMapping(value = "uploadFile",method = RequestMethod.POST)
19     public String uploadFile(@RequestParam MultipartFile uploadFile,
20                              HttpServletRequest request) throws IOException {
21 
22         String username = request.getParameter("username");
23         System.out.println("username = " + username);
24         String fileName = uploadFile.getOriginalFilename();
25         String targetPath = request.getSession().getServletContext().getRealPath("uploadFile");
26         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
27         String datetime = simpleDateFormat.format(new Date());
28         File file = new File(targetPath,datetime + "_" + System.nanoTime() + "_" + fileName);
29         uploadFile.transferTo(file);
30         return "success";
31     }
32 
33     @RequestMapping(value = "uploadMoreFile",method = RequestMethod.POST)
34     //注意:一定要加上@RequestParam("uploadFile")来关联form表单的name属性值,否则上传文件不起作用
35     public String uploadMoreFile(@RequestParam("uploadFile") MultipartFile[] uploadFiles,HttpServletRequest request) throws IOException {
36 
37         String username = request.getParameter("username");
38         System.out.println("username = " + username);
39         for(MultipartFile uploadFile:uploadFiles){
40             String fileName = uploadFile.getOriginalFilename();
41             String realPath = request.getSession().getServletContext().getRealPath("upload/images");
42             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
43             String getDateTime = simpleDateFormat.format(new Date());
44             File file = new File(realPath, getDateTime + "_" + System.nanoTime() + "_" + fileName);
45             uploadFile.transferTo(file);
46         }
47 
48         return "success";
49     }
50 
51 
52 }

六、效果图

  

 七、测试

  1.测试单文件上传

 

    2.多文件上传

 八、恭喜你,成功了!

   

原文地址:https://www.cnblogs.com/zui-ai-java/p/10233391.html