SpringMVC杂记(一) 文件上传
1) 添加multipartResolver的配置
2) 由于multipartResolver采用的是CommonsMultipartResolver的实现毫无疑问 Jakarta commons fileupload组件是必须添加到
CLASSPATH下的。 另外Spring也使用了 Jakarta commons io组件。
pom.xml
3) JSP
4) RequestMapping 方法
5) 上传文件过大怎么办? 如何抓到org.springframework.web.multipart.MaxUploadSizeExceededException ?
其实这样做是不行的。 因为,test()方法还没有被执行的时候,在数据绑定阶段MaxUploadSizeExceededException的一个实例已经被抛出。
SpringMVC框架,直接在exceptionResolver的配置中去找对应的逻辑视图名。
应该把这个Controller实现为一个org.springframework.web.servlet.HandlerExceptionResolver。
1) 添加multipartResolver的配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
2) 由于multipartResolver采用的是CommonsMultipartResolver的实现毫无疑问 Jakarta commons fileupload组件是必须添加到
CLASSPATH下的。 另外Spring也使用了 Jakarta commons io组件。
pom.xml
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
3) JSP
<%@ page language="java" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page isELIgnored="false" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>" />
<title>上传测试</title>
</head>
<body>
<form action="test/upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>文件名</td>
<td> <input type="text" name="fileName"> </td>
</tr>
<tr>
<td>文件</td>
<td> <input type="file" name="file"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="上传"></td>
</tr>
</table>
</form>
</body>
</html>
4) RequestMapping 方法
@Controller
@RequestMapping(value = "/test")
public class TestController {
private static final File USER_FILES_DIRECTORY = new File("D:/TEST_UPLOAD/");
@ResponseBody
@RequestMapping(value = "/upload", method = {RequestMethod.POST})
public String test(
@RequestParam("fileName") String fileName,
@RequestParam("file") CommonsMultipartFile file) throws Exception {
file.getFileItem().write(new File(USER_FILES_DIRECTORY, fileName));
return "OK";
}
}
5) 上传文件过大怎么办? 如何抓到org.springframework.web.multipart.MaxUploadSizeExceededException ?
@Controller
@RequestMapping(value = "/test")
public class TestController{
// ...
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ModelAndView handleMaxUploadSizeExceededException(Exception ex, WebRequest request) {
// TODO: 在这里处理MaxUploadSizeExceededException
return null;
}
}
其实这样做是不行的。 因为,test()方法还没有被执行的时候,在数据绑定阶段MaxUploadSizeExceededException的一个实例已经被抛出。
SpringMVC框架,直接在exceptionResolver的配置中去找对应的逻辑视图名。
应该把这个Controller实现为一个org.springframework.web.servlet.HandlerExceptionResolver。
@Controller
@RequestMapping(value = "/test")
public class TestController implements HandlerExceptionResolver {
private static final File USER_FILES_DIRECTORY = new File("D:/TEST_UPLOAD/");
@ResponseBody
@RequestMapping(value = "/upload", method = {RequestMethod.POST})
public String test(
@RequestParam("fileName") String fileName,
@RequestParam("file") CommonsMultipartFile file) throws Exception {
file.getFileItem().write(new File(USER_FILES_DIRECTORY, fileName));
return "OK";
}
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
if (ex instanceof MaxUploadSizeExceededException) {
System.out.println("上传过大错误已经处理");
return new ModelAndView("redirect:http://www.iteye.com");
}
return null; // 其他错误交由exceptionResolver处理
}
}