SpringMVC之单/多文件上传

1.准备jar包(图标所指必备包,其他按情况导入)

2.项目结构

3.SingleController.java(控制器代码单文件和多文件)

 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     //单文件上传
16     @RequestMapping(value="/upload.html")
17     public String upload(@RequestParam("uploadFile")MultipartFile file,HttpSession session) throws IllegalStateException, IOException{
18         //上传文件存储的路径
19 //        String filePath=session.getServletContext().getRealPath("upload");
20 //        System.out.println("存储路径"+filePath);
21         if(!file.isEmpty()){
22             //上传文件的全路径
23             File tempFile=new File("C:/pic"+"/"+file.getOriginalFilename());
24             System.out.println("全路径:"+tempFile);
25             //文件上传
26             file.transferTo(tempFile);
27         }
28         return "success";
29     } 
30     
31     //多文件上传
32     @RequestMapping(value="/upload.html")
33     public String upload(@RequestParam("uploadFile")MultipartFile[] files,HttpSession session) throws IllegalStateException, IOException{
34         //上传文件存储的路径
35 //        String filePath=session.getServletContext().getRealPath("upload");
36 //        System.out.println("存储路径"+filePath);
37         for(MultipartFile file:files){
38         if(!file.isEmpty()){
39             //上传文件的全路径
40             File tempFile=new File("C:/pic"+"/"+file.getOriginalFilename());
41             System.out.println("全路径:"+tempFile);
42             //文件上传
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         <!-- 指定编码格式 -->
20         <property name="defaultEncoding" value="utf-8"></property>
21         <!-- 上传文件的最大值 -->
22         <property name="maxUploadSize" value="10000000"></property>
23         <!-- 上传文件临时保存的位置 -->
24         <property name="uploadTempDir" value="tempDir"></property>
25     </bean>        
26     
27      
28 
29     <!-- 视图解析器。prefix:前缀, suffix:后缀 -->
30     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
31         <property name="prefix" value="/WEB-INF/jsp/"/>
32         <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.测试访问(单文件上传留一个input就行)

扫描二维码关注公众号,回复: 26208 查看本文章

9.注意

如果存储路径为Tomcat的安装路径,那么每次改动form.jsp会自动删除存储的文件夹,可以在控制器中修改存储路径为其他的。

猜你喜欢

转载自www.cnblogs.com/wtzl/p/8858411.html