转载自 : 兰馨岚雨 http://yanglei008.iteye.com/blog/246920
使用spring的multipartResolver来上传文件,设置resolveLazily=true,maxUploadSize=3145728,使用那个注解的方式@ExceptionHandler(Exception.class)来捕捉MaxUploadSizeExceededException异常
可以在controller(spring mvc) 或者 action (struts mvc)
中直接使用CommonsMultipartResolver
也可以在spring配置文件中配置
jsp code
中直接使用CommonsMultipartResolver
- ...Controller...{
- //创建一个通用的多部分解析器.
- CommonsMultipartResolver commonsMultipartResolver = new
- CommonsMultipartResolver(request.getSession().getServletContext());
- //设置编码
- commonsMultipartResolver.setDefaultEncoding(”utf-8″);
- //判断 request 是否有文件上传,即多部分请求...
- if (commonsMultipartResolver.isMultipart(request))
- {
- //转换成多部分request
- MultipartHttpServletRequest multipartRequest =
- commonsMultipartResolver.resolveMultipart(request);
- // file 是指 文件上传标签的 name=值
- // 根据 name 获取上传的文件...
- MultipartFile file = multipartRequest.getFile("file");
- //上传后记录的文件...
- File imageFile = new File("fileName");
- //上传...
- file.transferTo(imageFile);
- }
- }
也可以在spring配置文件中配置
- 也可以在Spring的配置文件中添加
- Xml代码
- <bean id=”multipartResolver” class=”org.springframework.web.multipart.commons.CommonsMultipartResolver”>
- <property name=”defaultEncoding” value=”UTF-8″ />
- </bean>
jsp code
- <form id=”fileUploadForm” method=”post” action=”XXX.do?method=fileUpload” enctype=”multipart/form-data”>
- <tr>
- <td>File:</td>
- <td><input type=”file” name=”file”></td>
- </tr>
- <tr>
- <td><input type=”hidden” name=”fileOwner” value=”1002″/></td>
- <td><input type=”submit” value=”submit”></td>
- </tr>
- </form>
==========================分割线==================================================================
另外一篇,也是关于Spring MVC上传文件的,也放到这里吧
转载自: http://mn960mn.blog.163.com/blog/static/11410308420109493440818/
package net.hqit.action;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUploadController {
@RequestMapping(value="/upload.do",method=RequestMethod.POST)
public String upload(@RequestParam("imageFile") MultipartFile image,HttpServletRequest request) throws IOException
{
String path = request.getRealPath("/upload");
request.setAttribute("name",image.getOriginalFilename());
request.setAttribute("img","<img src='http://mn960mn.blog.163.com/blog/upload/"+image.getOriginalFilename()+"'></img>");
FileCopyUtils.copy(image.getBytes(),new File(path+"/"+image.getOriginalFilename()));
return "ok";
}
}
spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="net.hqit.action"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
</beans>
别忘了加入commons里的fileupload跟io两个JAR包,这样就可以了。
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FileUploadController {
@RequestMapping(value="/upload.do",method=RequestMethod.POST)
public String upload(@RequestParam("imageFile") MultipartFile image,HttpServletRequest request) throws IOException
{
String path = request.getRealPath("/upload");
request.setAttribute("name",image.getOriginalFilename());
request.setAttribute("img","<img src='http://mn960mn.blog.163.com/blog/upload/"+image.getOriginalFilename()+"'></img>");
FileCopyUtils.copy(image.getBytes(),new File(path+"/"+image.getOriginalFilename()));
return "ok";
}
}
spring-servlet.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="net.hqit.action"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
</beans>
别忘了加入commons里的fileupload跟io两个JAR包,这样就可以了。