SpringMVC杂记(一) 文件上传

SpringMVC杂记(一) 文件上传

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处理
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值