springmvc文件上传

  1.spring-serlvet.xml  

1  <!-- 文件上传 -->
2      <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
3          <!-- 编码方式 -->
4         <property name="defaultEncoding" value="utf-8"></property>
5         <!-- 文件最大 -->
6         <property name="maxUploadSize" value="10485760000"></property> 
7         <!-- 缓存-->
8         <property name="maxInMemorySize" value="40960"></property>    
9      </bean>

注:这里bean的Id一定要写multipartResolver,前面不能加commons。

  2.Controller(一共有两种方法)

 1 @RequestMapping("/load")
 2     public String downLoad(
 3             @RequestParam("wenjian")CommonsMultipartFile multiFile,
 4             HttpServletRequest request) {
 5         if (!multiFile.isEmpty()) {
 6             File test = new File("D://upload");
 7             if(!test.exists()){
 8                 test.mkdirs();
 9             }
10             try {
11                 FileOutputStream out = new FileOutputStream("D://upload//"
12                         + multiFile.getOriginalFilename());
13                 InputStream in = multiFile.getInputStream();
14                 int len;
15                 byte[] buffer = new byte[1024 * 1024 * 10];
16                 while ((len = in.read(buffer)) > 0) {
17                     out.write(buffer, 0, len);
18                     out.flush();
19                 }
20                 out.close();
21                 in.close();
22             } catch (Exception e) {
23                 throw new RuntimeException(e);
24             }
25         }
26         return "/success";
27     }
28 
29     @RequestMapping("load1")
30     public String upload(HttpServletRequest request,HttpServletResponse response){
31         CommonsMultipartResolver  multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
32         
33         if (multipartResolver.isMultipart(request)) {
34             MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
35             
36             Iterator<String> iter = multipartHttpServletRequest.getFileNames();
37                 
38             while(iter.hasNext()){
39                 MultipartFile file = multipartHttpServletRequest.getFile(iter.next());
40                 System.out.println(file.getOriginalFilename()+"*-------------------");
41                 if(file != null){
42                     File test = new File("D://upload");
43                     if(!test.exists()){
44                         test.mkdirs();
45                     }
46                     File filePath = new File("D://upload/"+file.getOriginalFilename());
47                     try {
48                         file.transferTo(filePath);
49                     } catch (Exception e) {
50                         throw new RuntimeException(e);
51                     }
52                 }
53             }
54             
55         }
56         
57         return "/success";
58     }

 3.jsp

1 <form name="up" action="<%=request.getContextPath() %>/file/load" method="post"
2             enctype="multipart/form-data">
3             <input type="file" name="wenjian" />
4             <input type="submit" value="上传" />
5         </form>

   在做springmvc上传时需要两个jar文件

      1.com.springsource.org.apache.commons.fileupload-1.2.0.jar 

      2.com.springsource.org.apache.commons.io-1.4.0.jar 

   这两个jar文件有的时候名字会变commons.fileupload-1.2.0.jar ,commons.io-1.4.0.jar ,其实都一样

如果没有这两个jar包回报错:java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

如果没有multipart解析器回报这个错:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade  

 

 

 

 

posted @ 2014-01-08 14:03  哎呦喂,我的小祖宗╰つ  阅读(367)  评论(0编辑  收藏  举报