SpringMVC——文件上传
SpringMVC文件上传:
需要jar包:common-fileupload.jar common-io.jar
在SpringMVC中,文件上传功能可以由即插即用的CommonsMultipartResolver解析器组件实现,它在org.springframework.web.multipart包里。因此需要实例化此组件。
使用方法:
1.配置文件
<!-- 打开文件上载支持 id名字不能写其他的 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 限制上载文件大小为 100K byte
如超过100K会抛出异常-->
<property name="maxUploadSize" value="102400" />
</bean>
2,jsp页面中的表单设置:
a) 请求方式必须要用POST方式
b) enctype必须设置"multipart/form-data"
<body>
<h1>文件上传</h1>
<c:url var="url" value="/upload/file-upload.from"></c:url>
<form action="${url }" method="post" enctype="multipart/form-data">
图片:
<input type="file" name="image" />
<br />
作者:
<input type="text" name="author">
<br />
<input type="submit" value="提交">
</form>
</body>
3,Controller实现
注意:fileUpload(MultipartFile image, String author, ModelMap map,HttpServletRequest req) 这个方法中image的名字要和jsp页面中定义的一致。
或者使用:fileUpload(@RequestParam(value="image")MultipartFile image, ....)
@Controller
@RequestMapping("/upload")
public class UploadController {
// 显示上传页面
@RequestMapping("/toUpload.from")
public String toUploadForm() {
return "upload";
}
@RequestMapping("/file-upload.from")
public String fileUpload(MultipartFile image, String author, ModelMap map,
HttpServletRequest req) throws Exception {
// image.getOriginalFilename() //文件名字
// image.getName() //是上传的属性名称
// image.getBytes() //是上载的全部byte数据
// image.getInputStream() //获取文件数据流,
// image.getContentType() //获取文件的类型 image/jpg , image/png 等等
// 1,创建目标文件夹 /WEB-INF/images
String path = "/WEB-INF/images";
path = req.getSession().getServletContext().getRealPath(path);
System.out.println("实际路径:" + path);
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
String uuid = UUID.randomUUID().toString();
String filename = image.getOriginalFilename(); // 123.png
String img = uuid + filename.substring(filename.lastIndexOf(".")); // 获取文件名:uuid+".png"
String txt = uuid + ".txt";
// 写出图片文件
FileOutputStream imgOut = new FileOutputStream(new File(dir, img));
imgOut.write(image.getBytes());
imgOut.close();
// 写出元数据文本文件
PrintWriter out = new PrintWriter(new File(dir, txt));
out.println("原始文件名:" + filename);
out.println("作者:" + author);
out.println("ContentType:" + image.getContentType());
out.println(image.getBytes());
out.close();
// 返回到成功页面
map.put("msg", "成功上载" + filename);
return "success";
}
}
下面看完整的ApplicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.lydia.web" />
<!-- 支持@RequestMapping请求和Controller映射 -->
<mvc:annotation-driven />
<!-- 打开文件上载支持 id名字不能写其他的 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 限制上载文件大小为 100K byte
如超过100K会抛出异常-->
<property name="maxUploadSize" value="102400" />
</bean>
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/">
</property>
<property name="suffix" value=".jsp">
</property>
</bean>
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">error</prop>
<!--
<prop key="com.tarena.web.DemoException">error</prop>
-->
</props>
</property>
</bean>
</beans>
测试:如果上传尺寸过大,出现异常
