SpringMVC中实现文件上传需要两个jar包
主要是CommonsMultipartResolver解析器依赖commons-fileupload和commons-io这两个jar包
<!--文件上传 jar--> <!--fileupload--> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!--io--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.4</version> </dependency>
一。实现单文件上传
先准备页面
<%-- Created by IntelliJ IDEA. User: mycom Date: 2018/4/2 Time: 9:55 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>文件上传</title> </head> <body> <h1>文件上传</h1> <form action="/second" method="post" enctype="multipart/form-data"> 文件1 :<input type="file" name="upload"/> <input type="submit" value="提交"> </form> </body> </html>
在控制器类中
package demo19Fileupload.Fileupload01; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpSession; import java.io.File; import java.io.IOException; /** * Created by mycom on 2018/4/2. */ @Controller public class FirstController { @RequestMapping("/first") public String doFirst(MultipartFile upload, HttpSession session){ //upload就是客户端浏览器上传的文件对象 //获得文件名+后缀名 String childPath=upload.getOriginalFilename(); System.out.println("childPath:"+childPath); //绝对路径 //左半部分 String parentPath=session.getServletContext().getRealPath("/upload"); //创建一个file对象 File file=new File(parentPath,childPath); try { //将文件传送到你指定的文件目录中 upload.transferTo(file); } catch (IOException e) { e.printStackTrace(); return "fileupload"; } return "success"; } }
配置文件中
<?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"> <context:component-scan base-package="demo19Fileupload.Fileupload02"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/fileupload/"></property> <property name="suffix" value=".jsp"></property> </bean>
<!--主要是这个 配置一个多文件解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
<!--注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven> </beans>
不要忘记修改web.xml
还需要注意的是:在你的webapps下先创建一个文件夹。文件夹中最好放一个文件或者空文件,运行完成功之后,要看你编译后的文件,应该在你的target中项目下查找
二。多文件上传
页面上
<%-- Created by IntelliJ IDEA. User: mycom Date: 2018/4/2 Time: 9:55 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>文件上传</title> </head> <body> <h1>文件上传</h1> <form action="/second" method="post" enctype="multipart/form-data"> 文件1 :<input type="file" name="upload"/> 文件2 :<input type="file" name="upload"/> 文件3 :<input type="file" name="upload"/> <input type="submit" value="提交"> </form> </body> </html>
控制器中的方法
@RequestMapping("/second") public String doSecond(@RequestParam MultipartFile[] upload, HttpSession session){ for (MultipartFile item:upload) { if(item.getSize()>0){//如果大于0,那么就说明有文件,就执行文件上传的代码,<0就没有文件 //获得短路径 String childPath=item.getOriginalFilename(); //获得左半部分 String parentPath=session.getServletContext().getRealPath("/upload"); //创建一个File File file=new File(parentPath,childPath); try { item.transferTo(file); } catch (IOException e) { e.printStackTrace(); return "fileupload"; } }else{ return "fileupload"; } } return "success"; }
传入的参数变成文件数组,然后用foreach遍历
配置文件不变
这是单个文件上传和多个文件上传