使用spring MVC框架进行文件上传

2008-02-23 09:33:24来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

使用spring MVC框架进行文件上传,步骤如下:

1:配置Web.XML文件。定义DispatcherServlet,DispatcherServlet处理的请求(.htm)也在同一个web.xml文件里使用url-mapping定义映射。

<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>org.springFramework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
2:定义upload-servlet.xml文件。
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size100MB -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/upload.htm">uploadController</prop>
</props>
</property>
</bean>
<bean id="uploadController" class="FileUploadController">
<property name="commandClass"><value>FileUploadBean</value></property>
<property name="uploadDir"><value>E:/</value></property>
<property name="formView"><value>fail</value></property>
<property name="successView"><value>confirmation</value></property>
</bean>
3:定义控制类,commandClass及方法。控制类中最重要的方法是initBinder()它给spring注册了一个编辑器对
request中的multipart实体进行处理,如果没有这个方法,上传将不能进行。
<--------------------------控制类------------------->
public class FileUploadController extends SimpleFormController {
private static Log log =
LogFactory.getLog(FileUploadController.class);
private String uploadDir;//上传文件路径

protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object cmd, BindException errors)
throws Exception {

FileUploadBean bean = (FileUploadBean) cmd;
byte[] bytes = bean.getFile();

//cast to multipart file so we can get additional information
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");

String uploadDir = this.getUploadDir();

File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
String sep = System.getProperty("file.separator");
if (log.isDebugEnabled()) {
log.debug("uploading to: " uploadDir sep
file.getOriginalFilename());
}
File uploadedFile = new File(uploadDir sep
file.getOriginalFilename());
FileCopyUtils.copy(bytes, uploadedFile);
System.out.println("********************************");
System.out.println(uploadedFile.getAbsolutePath());
System.out.println(bytes.length);
System.out.println("********************************");


return new ModelAndView(getSuccessView() ".JSP");
}

protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws ServletException {
binder.reGISterCustomEditor(byte[].class,
new ByteArrayMultipartFileEditor());
}
public void setUploadDir(String uploadDir){
this.uploadDir = uploadDir;
}
public String getUploadDir(){
return this.uploadDir;
}
}
<--------------------------控制类------------------------->
<---------------------定义commandClass-------------------->
public class FileUploadBean {

private byte[] file;

public void setFile(byte[] file) {
this.file = file;
}

public byte[] getFile() {
return file;
}

}
<---------------------定义commandClass-------------------->
4:定义一个form表单index.jsp
<form method="post" action="upload.htm" enctype="multipart/form-data">

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:教您如何用Zeus实现XML-Java的数据绑定

下一篇:Eclipse In Action 1.2