SpringMVC single/multiple file upload

1. Prepare the jar package (the necessary package indicated by the icon, the others are imported according to the situation)

2. Project structure

3.SingleController.java (controller code single file and multiple files)

 1 package com.wt.uplaod;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import javax.servlet.http.HttpSession;
 7 
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.bind.annotation.RequestParam;
11 import org.springframework.web.multipart.MultipartFile;
12 
13 @Controller
14 public class SingleController {
15      // Single file upload 
16      @RequestMapping(value="/upload.html" )
 17      public String upload(@RequestParam("uploadFile")MultipartFile file,HttpSession session) throws IllegalStateException, IOException{
 18          // The path to store the uploaded file
 19  //         String filePath=session.getServletContext().getRealPath("upload");
 20  //         System.out.println("Storage path"+filePath); 
21          if (! file.isEmpty()){
 22              // The full path of the uploaded file 
23              File tempFile= new File("C:/pic"+"/"+ file.
getOriginalFilename ());24              System.out.println("full path: "+ tempFile);
 25              // File upload 
26              file.transferTo(tempFile);
 27          }
 28          return "success" ;
 29      } 
 30      
31      // Multiple file upload 
32      @RequestMapping( value="/upload.html" )
 33      public String upload(@RequestParam("uploadFile")MultipartFile[] files,HttpSession session) throws IllegalStateException, IOException{
 34          // Upload file storage path
 35  //        String filePath=session.getServletContext().getRealPath("upload");
 36  //         System.out.println("Storage path"+filePath); 
37          for (MultipartFile file:files){
 38          if (! file.isEmpty( )){
 39              // The full path of the uploaded file 
40              File tempFile= new File("C:/pic"+"/"+ file.getOriginalFilename());
 41              System.out.println("full path: "+ tempFile );
 42              // File upload 
43              file.transferTo(tempFile);
 44          }
 45          }
 46          return "success";
47     } 
48     
49     
50     @RequestMapping("/toFormPage.html")
51     public String toFormPage(){
52         return "form";
53     }
54 }
View Code

4.springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd
12         http://www.springframework.org/schema/mvc
13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
14         
15   <context:component-scan base-package="com.wt.uplaod"></context:component-scan>      
16   <mvc:annotation-driven></mvc:annotation-driven>
17         
18     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
19         <!-- Specify encoding format-->
 20          <property name="defaultEncoding" value="utf-8"></property>
 21          <!-- Maximum upload file size-->
 22          <property name="maxUploadSize " value="10000000"></property>
 23          <!-- The location where uploaded files are temporarily saved -->
 24          <property name="uploadTempDir" value="tempDir"></property>
 25      </bean>        
 26      
27       
28  
29      <!-- View resolver. prefix: prefix, suffix: suffix -->
 30      <bean class ="org.springframework.web.servlet.view.InternalResourceViewResolver" >
         <property name="suffix" value=".jsp"/>
33     </bean>
34 
35 </beans>
View Code

5.web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>SpringMVC</display-name>
 4   <servlet>
 5     <servlet-name>article6</servlet-name>
 6     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 7     <init-param>
 8       <param-name>contextConfigLocation</param-name>
 9       <param-value>classpath:springmvc-servlet.xml</param-value>
10     </init-param>
11   </servlet>
12   <servlet-mapping>
13     <servlet-name>article6</servlet-name>
14     <url-pattern>/url/*</url-pattern>
15   </servlet-mapping>
16 </web-app>
View Code

6.form.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="upload.html" method="post" enctype="multipart/form-data">
11 <input type="file" name="uploadFile"><br/>
12 <input type="file" name="uploadFile"><br/>
13 <input type="file" name="uploadFile"><br/>
14 <input type="submit" value="上  传">
15 </form>
16 </body>
17 </html>
View Code

7.success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 上传成功
11 </body>
12 </html>
View Code

8. Test access (just leave one input for single file upload)

9. Pay attention

If the storage path is the installation path of Tomcat, each time the form.jsp is changed, the stored folder will be automatically deleted, and the storage path can be changed to another one in the controller.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324488237&siteId=291194637