springMVC的文件上传


一、工程目录结构

二、web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  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     
10 
11     
12         encodingFilter
13         class>org.springframework.web.filter.CharacterEncodingFilterclass>
14             
15                 encoding
16                 utf-8
17             
18     
19 
20     
21         encodingFilter
22         /*
23     
24     
25     
26     
27 
28     
29         dispatcherServlet
30         org.springframework.web.servlet.DispatcherServlet
31             
32                 contextConfigLocation
33                 classpath:spring/springMVC.xml
34             
35     
36     
37         dispatcherServlet
38         /
39     
40     
41 

三、springMVC.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  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     package="com.wang.mvc.controller"/>
 9     
10     
11     class="org.springframework.web.servlet.view.InternalResourceViewResolver"
12           p:prefix="/jsp/" p:suffix=".jsp"/>
13     
14     
16 
17     id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
18         
19     
20     
21     
22     
23 

四、前端页面

  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 
10 
11     Login
12 
13 
14     Login page...
15     
16 ------单文件上传------- 17
18
enctype="multipart/form-data"> 19 username: 20
21 file: 22
23 24
25 26
27 ------多文件上传------- 28
29
enctype="multipart/form-data"> 30 username: 31
32 file1: 33
34 file2: 35
36 file3: 37
38 39
40 41

       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" %>


    Success


    Success page...
    
文件上传成功!

五、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.多文件上传

 八、恭喜你,成功了!