其他分享
首页 > 其他分享> > 九、springMVC之文件上传

九、springMVC之文件上传

作者:互联网

一、配置方式

web.xml:

只配置了DispatcherServlet:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5     id="WebApp_ID" version="3.1">
 6     <servlet>
 7         <servlet-name>springDispatcherServlet</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <!-- 配置DispatcherServletd 一个初始化参数:配置springmvc配置文件的位置和名称 -->
10         <!-- 实际上也可以不通过 contextConfigLocation 来配置Springmvc的配置文件,而是用默认的 即默认的配置文件为 
11             /WEB-INF/<servlet-name>-servlet.xml 本项目默认位置配置文件即为: /WEB-INF/springDispatcherServlet-servlet.xml -->
12         <init-param>
13             <param-name>contextConfigLocation</param-name>
14             <param-value>classpath:spring.xml</param-value>
15         </init-param>
16         <!-- 表示springDispatcherServlet在加载的时候被创建 -->
17         <load-on-startup>1</load-on-startup>
18     </servlet>
19 
20     <!-- Map all requests to the DispatcherServlet for handling -->
21     <servlet-mapping>
22         <servlet-name>springDispatcherServlet</servlet-name>
23         <url-pattern>/</url-pattern>
24     </servlet-mapping>
25 </web-app>
View Code

 

spring.xml:基础配置+CommonsMultipartResolver配置

基础配置包括:包扫描的配置、视图解析器配置、<mvc:annotation-driven></mvc:annotation-driven>;

 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:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <context:component-scan base-package="handler"></context:component-scan>
11     <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 -->
12     <bean
13         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14         <property name="prefix" value="/WEB-INF/views/"></property>
15         <property name="suffix" value=".jsp"></property>
16     </bean>
17         
18     <mvc:annotation-driven></mvc:annotation-driven>
19     
20     <bean id="multipartResolver"
21         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
22         <property name="defaultEncoding" value="UTF-8"></property>
23         <property name="maxUploadSize" value="1024000"></property>
24     </bean>
25     
26 </beans>

 

 

二、实例验证:

1、目录结构

 

2.加入jar包:

springMVC基础jar包+commons-fileupload+commons-io,即:

3.EmployeeController.java

如果文件上传成功,跳转到success.jsp页面;

 1 package handler;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import org.springframework.web.bind.annotation.RequestParam;
 6 import org.springframework.web.multipart.MultipartFile;
 7 
 8 @RequestMapping("/emp")
 9 @Controller
10 public class EmployeeController {
11 
12     @RequestMapping("/testFileUpload")
13     public String testFileUpload(@RequestParam("files") MultipartFile[] files) {
14         for (int i = 0; i < files.length; i++) {
15             System.out.println(files[i].getOriginalFilename());
16         }
17         return "success";
18     }
19 }

4.index.jsp:

测试多个文件上传;

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@page import="java.util.*"%>
 4 <!DOCTYPE html>
 5 <html>
 6 <head>
 7 <meta charset="UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <form action="emp/testFileUpload" method="Post"
12         enctype="multipart/form-data">
13         Files多个文件上传:<input type="file" multiple="multiple" name="files"> 
14          <input type="submit" value="submit">
15     </form>    
16     
17 </body>
18 </html>

5.success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 success
11 </body>
12 </html>
View Code

6.运行结果:

选择多个文件上传后,跳转到success.jsp页面且控制台打印选中的多个文件的文件名。

 

标签:xml,文件,5.3,10,springMVC,spring,jar,web,上传
来源: https://www.cnblogs.com/lixiuming521125/p/16025804.html