Spring由内置的multipart支持web应用中的文件上传。multipart支持的设计是通过定义org.springframework.web.multipart包中的插件对象MultipartResovler来完成的。Spring提供MultipartResolver可以支持Commons FileUpload (http://jakarta.apache.org/commons/fileupload)和COS FileUpload (http://www.servlets.com/cos)。本章后面的部分描述了文件上传是如何支持的。
缺省,Spring是没有multipart处理,因为一些开发者想要自己处理它们。如果你想使用Spring的multipart,需要在web应用的上下文中添加multipart解析器。这样,每个请求就会被检查是否包含multipart。然而,如果请求中包含multipart,你的上下文中定义的MultipartResolver就会解析它。这样,你请求中的multipart属性就会象其它属性一样被处理。
使用MultipartResolver
下面的例子说明了如何使用CommonsMultipartResolver:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maximumFileSize">
<value>100000</value>
</property>
</bean>---------------------------------------------------------------------------------------------------下面这部分是我的配置文件部分------------------------------ <!-- 配置上传文件--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize"> <value>1000000</value> </property> </bean> <bean id="uploadControl" class="com.f9inux.controller.uploadController"> <property name="commandClass"> <value>com.f9inux.bean.uploadfilebean</value> </property> <property name="successView"> <value>index</value> </property> <property name="path"> <value>g:\web</value> </property> </bean> <!-- 配置上传文件-->-----------------上面由于我使用了springMVC如果有不明白的可以给我留言-----------------------------uploadController部分(类似于struts的action)-----------------package com.f9inux.controller;import java.io.BufferedOutputStream;import java.io.FileOutputStream;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.validation.BindException;import org.springframework.web.bind.ServletRequestDataBinder;import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;import com.f9inux.bean.uploadfilebean;public class uploadController extends SimpleFormController { protected String path; public void setPath(String path) { this.path = path; } public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command,BindException errors)throws Exception{ uploadfilebean ub=(uploadfilebean)command; //System.out.println(ub.getName()); //String sp=path+System.getProperty("file.separator")+ub.getName(); String sp=path+"\\"+ub.getName(); //System.out.println(sp);//输出存放目录 BufferedOutputStream bf=new BufferedOutputStream(new FileOutputStream(sp)); bf.write(ub.getContents()); bf.close(); return new ModelAndView(this.getSuccessView()); } public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder)throws Exception{ super.initBinder(request,binder); binder.registerCustomEditor( byte[].class,new ByteArrayMultipartFileEditor()); }}-----------------------上面主要实现的保存文件的逻辑---------------------------------------------uploadfilebean部分(这部分主要负责表单内容的承载,类似实体bean)--------package com.f9inux.bean;public class uploadfilebean { private String name; private byte[] contents; public byte[] getContents() { return contents; } public String getName() { return name; } public void setContents(byte[] contents) { this.contents = contents; } public void setName(String name) { this.name = name; }}------------------上面的属性和表单的控件相对应---------------------------------表单部分------------------- <form method="post" action="upload.do" enctype="multipart/form-data"> <input type="file" name="contents" οnchange="getvalue()"/> <input type="hidden" name="name"/> <input type="submit"/> </form>关于enctype部分的解释:使用multipart,你需要创建一个带文件上传域的表单,让Spring将文件绑定到你的表单上------------------------------------------最后请在你的tomcat的command/lib目录中添加commons-io.jar、commons-fileupload.jar包~~!
web.xml片段:
- <servlet>
- <servlet-name>springServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springServlet</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
servlet.xml片段:
- <bean id="multipartResolver"
- class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- 设置上传文件的最大尺寸为10MB -->
- <property name="maxUploadSize">
- <value>100010485761</value>
- </property>
- </bean>
Controller:
- @RequestMapping(params = "method=upload")
- protected ModelAndView uploadFile(HttpServletRequest request)
- throws Exception {
- try{
- //cast to multipart file so we can get additional information
- MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
- CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");
- System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
- byte[] bytes = file.getBytes();
- String uploadDir = "c:\\uploadFile";
- System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + uploadDir);
- 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("********************************");
- }catch(Exception e){
- e.printStackTrace();
- }
- return new ModelAndView("");
- }
页面:
- <form method="post" action="collParam.do?method=upload" enctype="multipart/form-data">
- <input type="file" name="file" id="file"/>
- <input type="submit" value="save"/>
- </form>