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

来源:互联网 发布:淘宝买到假烟 编辑:程序博客网 时间:2024/05/03 09:48

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

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

[html] view plaincopyprint?
  1. <servlet>  
  2.   <servlet-name>upload</servlet-name>  
  3.   <servlet-class>org.springframework.web.servlet.DispatcherServlet  
  4.   </servlet-class>  
  5.   <load-on-startup>1</load-on-startup>  
  6.  </servlet>  

[html] view plaincopyprint?
  1. <servlet-mapping>  
  2.   <servlet-name>upload</servlet-name>  
  3.   <url-pattern>*.htm</url-pattern>  
  4.  </servlet-mapping>  

2:定义upload-servlet.xml文件。

[html] view plaincopyprint?
  1. <bean id="multipartResolver"  
  2.        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  3.         <!-- set the max upload size100MB -->  
  4.         <property name="maxUploadSize">  
  5.         <value>104857600</value>  
  6.     </property>  
  7.     <property name="maxInMemorySize">  
  8.         <value>4096</value>  
  9.     </property>  
  10.    </bean>  
  11.  <bean id="urlMapping"  
  12.   class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">         
  13.  <property name="mappings">             
  14.   <props>                 
  15.   <prop key="/upload.htm">uploadController</prop>             
  16.   </props>         
  17.  </property>     
  18.  </bean>  
  19.      <bean id="uploadController" class="FileUploadController">  
  20.       <property name="commandClass"><value>FileUploadBean</value></property>  
  21.       <property name="uploadDir"><value>E:/</value></property>  
  22.       <property name="formView"><value>fail</value></property>  
  23.   <property name="successView"><value>confirmation</value></property>  
  24. </bean>    
3:定义控制类,commandClass及方法。控制类中最重要的方法是initBinder()它给spring注册了一个编辑器对
request中的multipart实体进行处理,如果没有这个方法,上传将不能进行。

<--------------------------控制类------------------------->

[java] view plaincopyprint?
  1. public class FileUploadController extends SimpleFormController {  
  2.     private static Log log =  
  3.         LogFactory.getLog(FileUploadController.class);  
  4.     private String uploadDir;//上传文件路径  
  5.   
  6.     protected ModelAndView onSubmit(HttpServletRequest request,  
  7.             HttpServletResponse response, Object cmd, BindException errors)  
  8.             throws Exception {  
  9.   
  10.             FileUploadBean bean = (FileUploadBean) cmd;  
  11.             byte[] bytes = bean.getFile();  
  12.             
  13.             //cast to multipart file so we can get additional information  
  14.             MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;  
  15.             CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");  
  16.   
  17.             String uploadDir = this.getUploadDir();  
  18.   
  19.             File dirPath = new File(uploadDir);  
  20.             if (!dirPath.exists()) {  
  21.                 dirPath.mkdirs();  
  22.             }  
  23.             String sep = System.getProperty("file.separator");  
  24.             if (log.isDebugEnabled()) {  
  25.                 log.debug("uploading to: " + uploadDir + sep +  
  26.                 file.getOriginalFilename());  
  27.                 }  
  28.             File uploadedFile = new File(uploadDir + sep  
  29.                     + file.getOriginalFilename());  
  30.             FileCopyUtils.copy(bytes, uploadedFile);  
  31.             System.out.println("********************************");  
  32.             System.out.println(uploadedFile.getAbsolutePath());  
  33.             System.out.println(bytes.length);  
  34.             System.out.println("********************************");  
  35.              
  36.        
  37.         return new ModelAndView(getSuccessView() + ".jsp");  
  38.     }  
  39.   
  40.     protected void initBinder(HttpServletRequest request,  
  41.             ServletRequestDataBinder binder) throws ServletException {  
  42.         binder.registerCustomEditor(byte[].class,  
  43.                 new ByteArrayMultipartFileEditor());  
  44.     }  
  45.     public void setUploadDir(String uploadDir){  
  46.         this.uploadDir = uploadDir;  
  47.     }  
  48.     public String getUploadDir(){  
  49.         return this.uploadDir;  
  50.     }  
  51. }  
<---------------------定义commandClass-------------------->
[java] view plaincopyprint?
  1. public class FileUploadBean {  
  2.   
  3.     private byte[] file;  
  4.   
  5.     public void setFile(byte[] file) {  
  6.         this.file = file;  
  7.     }  
  8.   
  9.     public byte[] getFile() {  
  10.         return file;  
  11.     }  
  12.   
  13. }  

4:定义一个form表单index.jsp

[plain] view plaincopyprint?
  1. <form method="post" action="upload.htm" enctype="multipart/form-data">  
  2. <input type="file" name="file"/>  
  3. <input type="submit"/>  
  4. </form>  
5:定义confirmation.jsp及fail.jsp

confirmation.jsp如下:

[plain] view plaincopyprint?
  1. <%@ page contentType="text/html; charset=GBK" %>  
  2. <html>  
  3. <head>  
  4. <title>  
  5. successView  
  6. </title>  
  7. </head>  
  8. <body bgcolor="#ffffff">  
  9. <h1>  
  10. Upload Successful  
  11. </h1>  
  12. </body>  
  13. </html>  

fail.jsp如下:

[plain] view plaincopyprint?
  1. <html>  
  2. <head>  
  3. <title>Upload a file please</title>  
  4. </head>  
  5. <body>  
  6. <h1>no file,Please upload a file</h1>  
  7. <form method="post" action="uploadfile.htm" enctype="multipart/form-data">  
  8. <input type="file" name="file"/>  
  9. <input type="submit"/>  
  10. </form>  
  11. </body>  
  12. </html>  

6:运行tomcat。
预览 ie里面:http://localhost/springmvc/index.jsp
注:
a:文件目录为tomcat-HOME/webapps/springmvc/
.jsp文件都放在根目录下,.class文件放在/WEB-INF/classes/中

其他文件放在/WEB-INF/里面。
b:如果连上面的你都有疑问,那还是去看看spring的基础知识吧。


0 0