<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="1024000"></property>
</bean>
- 如果设置了文件的最大限制,则需要在配置文件中添加如下代码,来指定超出限制时所跳转的错误页面:
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">error</prop>
</props>
</property>
</bean>
<%--
Created by IntelliJ IDEA.
User: elin
Date: 15-7-4
Time: 下午7:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<form action="<%=request.getContextPath()%>/user/uploadHandle" method="post" enctype="multipart/form-data">
描述:<input type="text" name="desc">
上传文件:<input type="file" name="file" multiple="multiple">
<input type="submit" value="上传">
</form>
</body>
</html>
@RequestMapping("/uploadHandle")
public String uploadHandle(@RequestParam("desc") String desc,@RequestParam("file") MultipartFile[] multipartFiles) throws Exception{
for (MultipartFile multipartFile : multipartFiles) {
String path = "/home/elin/workspace/upload/";
String oldFileName = multipartFile.getOriginalFilename();
String newFileName = UUID.randomUUID() + oldFileName.substring(oldFileName.lastIndexOf("."));
File file = new File(path + newFileName);
multipartFile.transferTo(file);
}
return "success";
}