第一步:
导入jar包:
第二步:
在springMVC的配置环境中添加文件上传解析器,这里可以设置上传的默认格式(解决 中文乱码问题),可以限制上传文件的大小,都可以在这里进行设置:
<!-- 自定义文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传的默认编码格式-->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 显示文件上传的大小-->
<property name="maxUploadSize" value="30"></property>
</bean>
第三步,
书写一个简易的页面,这里可以用作参考:
这里的form表单必须要设置enctype,文件上传的方式必须要用到post,因为get的长度有限制,当文件过大的时候无法进行上传
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="/demo" method="post" enctype="multipart/form-data">
<p>
姓名: <input type="text" id="uname">
</p>
<p>
年龄: <input type="text" id="age">
</p>
<p>
上传: <input type="file" name="fil">
</p>
<p>
<input type="submit" value="上传">
</p>
</form>
</body>
</html>
第四步:
编写controller,接受参数,处理文件:
package com.bjsxt.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Controller
public class MyController {
@RequestMapping("/demo")
public String demo( MultipartFile fil, HttpServletRequest req) throws IOException {
// 设定文件的存储路径,如果文件不存在就创建文件
File file=new File(req.getServletContext().getRealPath("/img"));
if(!file.exists()){
file.mkdir();
}
// getName是表单的name,getOriginalFilename是获得文件全名 getSize是获得文件大小 getContentType是获得文件路径
System.out.println(fil.getName()+"---"+fil.getOriginalFilename()+"--"+fil.getSize()+"-"+fil.getContentType());
// 因为重复的文件名会替换之前的文件名,所以使用UUID,可以防止文件名重复
String uuid = UUID.randomUUID().toString();
String jgp = fil.getOriginalFilename().substring(fil.getOriginalFilename().lastIndexOf("."));
// 新的文件名 uuid.jgp
String fileName=uuid+jgp;
// 文件转换,将文件存储转换到file里,名字是fileName
fil.transferTo(new File(file,fileName));
return "redirect:success.jsp";
}
}
完整的mvc文件:
最后一个是异常解析器,当文件大小出现了异常,就会跳到error.jsp页面
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:resources mapping="/img/**" location="/img/"></mvc:resources>
<!-- 自定义文件上传解析器-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置文件上传的默认编码格式-->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 显示文件上传的大小-->
<property name="maxUploadSize" value="30"></property>
</bean>
<!-- 自定义资源异常解析器-->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 异常映射-->
<property name="exceptionMappings">
<props>
<!--出现异常的信息的全路径这个异常一定写Spring抛出的异常,里面写出现异常的页面-->
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error.jsp</prop>
</props>
</property>
</bean>
</beans>