springmvc文件上传
1.创建maven的web工程,结构如下
2.各文件内容
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lgq</groupId>
<artifactId>testweb</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
</dependencies>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--设置访问项目的首页-->
<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
upload.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上传附件</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/upload/single" method="post" enctype="multipart/form-data">
名称<input type="text" name="username"><br/>
单文件1<input type="file" name="uploadFile"><br/>
单文件2<input type="file" name="uploadFile2"><br/>
<input type="submit" value="提交">
</form>
<br/>
<br/>
<br/>
<form action="${pageContext.request.contextPath}/upload/multi" method="post" enctype="multipart/form-data">
名称<input type="text" name="username"><br/>
多文件1<input type="file" name="uploadFile"><br/>
多文件2<input type="file" name="uploadFile"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
UploadController.java
PS:提前建立好basePath目录(本文即“D:\MyUtil\Test\upload\”)
package com.lgq.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
@Controller
@RequestMapping("/upload")
public class UploadController {
//单文件上传
@RequestMapping(value = "/single")
@ResponseBody
public void save22(String username, MultipartFile uploadFile, MultipartFile uploadFile2) throws IOException {
System.out.println(username);
String basePath = "D:\\MyUtil\\Test\\upload\\";
//获得上传文件的名称
String originalFilename = uploadFile.getOriginalFilename();
uploadFile.transferTo(new File(basePath + originalFilename));
String originalFilename2 = uploadFile2.getOriginalFilename();
uploadFile2.transferTo(new File(basePath + originalFilename2));
}
//多文件上传
@RequestMapping(value = "/multi")
@ResponseBody
public void save23(String username, MultipartFile[] uploadFile) throws IOException {
System.out.println(username);
String basePath = "D:\\MyUtil\\Test\\upload\\";
for (MultipartFile multipartFile : uploadFile) {
//获得上传文件的名称
String originalFilename = multipartFile.getOriginalFilename();
multipartFile.transferTo(new File(basePath + originalFilename));
}
}
}
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--Controller的组件扫描-->
<context:component-scan base-package="com.lgq">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"/><!--文件编码格式-->
<property name="maxUploadSize" value="10485760"/><!--最大上传大小10*1024*1024字节=10M-->
</bean>
</beans>