码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC单文件上传

时间:2014-08-31 23:03:22      阅读:382      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   使用   java   

        最近在看SpringMVC的文件上传部分,其实大多数系统都要涉及到文件上传,以前对于Struts的文件上传功能也做过总结了,今天主要说明一下如何使用SpringMVC进行表单上的文件上传以及多个文件同时上传的步骤。

一、设置配置文件:

        SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file

  1. <!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->  
  2.     <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
  3.         p:defaultEncoding="UTF-8"  
  4.         p:maxUploadSize="5000000"  
  5.      >  
  6.     </beans:bean>  

二、创建上传表单:

  1. <body>  
  2. <h2>文件上传</h2>  
  3. <form action="fileUpload.html" method="post" enctype="multipart/form-data">  
  4.     选择文件:<input type="file" name="file">  
  5.     <input type="submit" value="提交">   
  6. </form>  
  7. </body>  
        注意要在form标签中加上enctype="multipart/form-data"表示该表单是要处理文件类型。


三、编写上传控制类

1、创建一个控制类: FileUploadController和一个返回结果的页面list.jsp
2、编写提交表单的action:

  1. //通过Spring的autowired注解获取spring默认配置的request  
  2.     @Autowired  
  3.     private HttpServletRequest request;  
  4.   
  5.     /*** 
  6.      * 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile 
  7.      *  
  8.      * @param file 
  9.      * @return 
  10.      */  
  11.     @RequestMapping("fileUpload")  
  12.     public String fileUpload(@RequestParam("file") MultipartFile file) {  
  13.         // 判断文件是否为空  
  14.         if (!file.isEmpty()) {  
  15.             try {  
  16.                 // 文件保存路径  
  17.                 String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"  
  18.                         + file.getOriginalFilename();  
  19.                 // 转存文件  
  20.                 file.transferTo(new File(filePath));  
  21.             } catch (Exception e) {  
  22.                 e.printStackTrace();  
  23.             }  
  24.         }  
  25.         // 重定向  
  26.         return "redirect:/list.html";  
  27.     }  
  28.   
  29.     /*** 
  30.      * 读取上传文件中得所有文件并返回 
  31.      *  
  32.      * @return 
  33.      */  
  34.     @RequestMapping("list")  
  35.     public ModelAndView list() {  
  36.         String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";  
  37.         ModelAndView mav = new ModelAndView("list");  
  38.         File uploadDest = new File(filePath);  
  39.         String[] fileNames = uploadDest.list();  
  40.         for (int i = 0; i < fileNames.length; i++) {  
  41.             //打印出文件名  
  42.             System.out.println(fileNames[i]);  
  43.         }  
  44.         return mav;  
  45.     }  

3、使用SpringMVC注解RequestParam来指定表单中的file参数;
4、指定一个用于保存文件的web项目路径
5、通过MultipartFile的transferTo(File dest)这个方法来转存文件到指定的路径。
        到此基本的文件上传就结束了,当然,除了单个文件的上传之外,我们有的时候还需要同时上传多个文件,下篇文章我将继续讲解如何同时上传多个文件。

SpringMVC单文件上传

标签:des   style   blog   http   color   os   io   使用   java   

原文地址:http://blog.csdn.net/libaoqiang613/article/details/38963793

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!