SpringMVC使用进阶-文件上传

来源:互联网 发布:java程序设计课后答案 编辑:程序博客网 时间:2024/06/05 07:31

如今文件上传的方式已经是遍地开花,各种五花八门的文件上传有的时候着实让人难以抉择,如果是使用springmvc也不用为文件上传而发愁,springmvc的文件上传比struts2的那个还要简单,就是寥寥无几的一点代码就能解决上传。

1  修改之前的配置文件

spring主要是编写配置文件比较麻烦,配置文件正确是程序正确执行的关键,同样的springmvc如果要支持文件上传也需要加上如下配置;

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!-- 文件上传相关配置 -->  
  2.     <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >    
  3.         <property name="defaultEncoding" value="gbk"/> <!-- 默认编码 (ISO-8859-1) -->    
  4.         <property name="maxInMemorySize" value="10240"/> <!-- 最大内存大小 (10240)-->    
  5.         <property name="uploadTempDir" value="/temp/"/> <!-- 上传后的目录名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->    
  6.         <property name="maxUploadSize" value="-1"/> <!-- 最大文件大小,-1为无限止(-1) -->    
  7.     </bean>  


2  文件上传界面

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>文件上传</title>  
  8. </head>  
  9. <body>  
  10. <form action="upload.do"  method="post" enctype="multipart/form-data">  
  11.     名称:<input type="text" name="name" /><br><br>  
  12.     选择文件:<input type="file" name="image" /><br><br>  
  13.     <input type="submit" value="确认上传"/>  
  14. </form>  
  15. </body>  
  16. </html>  

无论jsp、php、asp.net上传文件一般都通过表单上传,而且还必须有enctype="multipart/form-data",否则文件将无法上传成功,还需要注意的一点就是springmvc的上传实现使用的是commons-io,因此相应的jar包也不能少。


3  文件上传控制器

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package org.hncst.controller;  
  2.   
  3. import java.io.File;  
  4. import java.util.Date;  
  5.   
  6. import javax.servlet.ServletContext;  
  7.   
  8. import org.springframework.stereotype.Component;  
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11. import org.springframework.web.bind.annotation.RequestMethod;  
  12. import org.springframework.web.bind.annotation.RequestParam;  
  13. import org.springframework.web.context.ServletContextAware;  
  14. import org.springframework.web.multipart.commons.CommonsMultipartFile;  
  15.   
  16. @Controller  
  17. public class FileUploadController  implements ServletContextAware{  
  18.     private ServletContext servletContext;  
  19.     public ServletContext getServletContext() {  
  20.         return servletContext;  
  21.     }  
  22.   
  23.     public void setServletContext(ServletContext servletContext) {  
  24.         this.servletContext = servletContext;  
  25.     }  
  26.     @RequestMapping(value="/upload.do", method = RequestMethod.POST)  
  27.     public String uploadFile(String name,@RequestParam("image") CommonsMultipartFile file) {  
  28.         String path=this.servletContext.getRealPath("/upload");  
  29.         //取得原始的文件名  
  30.         String fileName=file.getOriginalFilename();  
  31.         //取得文件的后缀(包含点号)  
  32.         String fileExt=fileName.substring(fileName.lastIndexOf("."));  
  33.         //使用时间戳给文件命名  
  34.         File file2=new File(path,new Date().getTime()+fileExt);  
  35.         //将上传的文件保存起来  
  36.         try {  
  37.             file.getFileItem().write(file2);  
  38.         } catch (Exception e) {  
  39.           
  40.             e.printStackTrace();  
  41.               
  42.             return "redirect:uploadFailure.jsp";  
  43.         }  
  44.         return "redirect:uploadSuccess.jsp";  
  45.     }  
  46.   
  47.       
  48. }  
0 0
原创粉丝点击