SpringMVC的文件上传

来源:互联网 发布:缠论君软件下载 编辑:程序博客网 时间:2024/03/29 14:47
 在该示例中,阐述了SpringMVC如何上传文件。
1、上传页面的jsp
  1. <body>  
  2.     <form action="/TestSpringMVC3/data/uploadfile" enctype="multipart/form-data" method="post">  
  3.         file:<input type="file" name="file"><br>  
  4.         <input type="submit" value="upload file">  
  5.     </form>  
  6. </body>  
2、controller配置文件
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.        xmlns:p="http://www.springframework.org/schema/p"  
  6.        xmlns:context="http://www.springframework.org/schema/context"  
  7.        xmlns:aop="http://www.springframework.org/schema/aop"  
  8.        xmlns:tx="http://www.springframework.org/schema/tx"  
  9.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  10.             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  11.             http://www.springframework.org/schema/context   
  12.             http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  13.             http://www.springframework.org/schema/aop   
  14.             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  15.             http://www.springframework.org/schema/tx   
  16.             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  17.             http://www.springframework.org/schema/mvc   
  18.             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
  19.             http://www.springframework.org/schema/context   
  20.             http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  21.     <!-- 
  22.         使Spring支持自动检测组件,如注解的Controller 
  23.     -->  
  24.     <context:component-scan base-package="cn.com.yy.controller"/>  
  25.       
  26.     <!-- 开启注解配置 -->  
  27.     <mvc:annotation-driven/>  
  28.          
  29.     <!-- 支持JSP JSTL的解析器 -->  
  30.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  31.         <property name="prefix" value="/WEB-INF/page/"/>  
  32.         <property name="suffix" value=".jsp"/>  
  33.      </bean>  
  34.        
  35.      <!-- 配置文件上传解析器 -->  
  36.      <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  37.         <property name="defaultEncoding" value="utf-8"/>  
  38.         <property name="maxUploadSize" value="10485760000"/>  
  39.         <property name="maxInMemorySize" value="40960"/>  
  40.      </bean>  
  41. </beans>  
    主要是添加了文件上传的解析器,配置了默认编码,最大的上传大小以及缓存大小等参数。
3、Controller
  1. package cn.com.yy.controller;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9.   
  10. import org.springframework.stereotype.Controller;  
  11. import org.springframework.web.bind.annotation.RequestMapping;  
  12. import org.springframework.web.bind.annotation.RequestParam;  
  13. import org.springframework.web.multipart.commons.CommonsMultipartFile;  
  14.   
  15. @Controller  
  16. @RequestMapping("/data")  
  17. public class FileUploadController {  
  18.       
  19.     /** 
  20.      * method1:通过参数CommonsMultipartFile进行解析 
  21.      * @RequestParam("file")中的file对应于upload.jsp中的file类型的name对应的名称 
  22.      * @param file 
  23.      * @return 
  24.      * @throws IOException  
  25.      */  
  26.     @RequestMapping(value="/uploadfile")  
  27.     public String upload1(@RequestParam("file") CommonsMultipartFile file) throws IOException{  
  28.         //获取文件名称  
  29.         String fileName = file.getOriginalFilename();  
  30.         //写入本地磁盘  
  31.         InputStream is = file.getInputStream();  
  32.         byte[] bs = new byte[1024];  
  33.         int len;  
  34.         OutputStream os = new FileOutputStream(new File("D:/temp/" + fileName));  
  35.         while ((len = is.read(bs)) != -1) {  
  36.             os.write(bs, 0, len);  
  37.         }  
  38.         os.close();  
  39.         is.close();  
  40.         return "upload_success";  
  41.     }  
  42.       
  43.     @RequestMapping("/upload")  
  44.     public String toPage(){  
  45.         return "upload";  
  46.     }  
  47. }  

4、返回页面upload_success.jsp
  1. <body>  
  2.     upload file success!!  
  3. </body>  

5、测试
     访问  http://localhost:8080/TestSpringMVC3/data/upload  跳转到上传页面
    
    选择文件上传
                    
  点击上传会跳转到上传成功页面。
      上述方法只是简单的讲解了SpringMVC如何上传文件。
    
  第二种方法:使用SpringMVC封装的方法进行文件上传
  1. /** 
  2.      * 使用SpringMVC封装好的方法进行文件上传 
  3.      * @param request 
  4.      * @param response 
  5.      * @throws IllegalStateException 
  6.      * @throws IOException 
  7.      */  
  8.     @RequestMapping("/uploadfile2")  
  9.     public void upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{  
  10.         //获取解析器  
  11.         CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
  12.         //判断是否是文件  
  13.         if(resolver.isMultipart(request)){  
  14.             //进行转换  
  15.             MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);  
  16.             //获取所有文件名称  
  17.             Iterator<String> it = multiRequest.getFileNames();  
  18.             while(it.hasNext()){  
  19.                 //根据文件名称取文件  
  20.                 MultipartFile file = multiRequest.getFile(it.next());  
  21.                 String fileName = file.getOriginalFilename();  
  22.                 String localPath = "D:/temp/" + fileName;  
  23.                 File newFile = new File(localPath);  
  24.                 //上传的文件写入到指定的文件中  
  25.                 file.transferTo(newFile);  
  26.             }  
  27.         }  
  28.     }  
   该方法上传文件效率更优
原创粉丝点击