angularjsFileUpload+Springmvc上传文件

来源:互联网 发布:手机淘宝店招尺寸多少 编辑:程序博客网 时间:2024/05/29 12:47

1.html页面以及js来自于angularjsFileUpload的demo

multiple属性为允许多个文件同时上传


参数设置

url为文件上传路径

formData属性对应的是参数数组,这里以传递id为例


2.springmvc后台

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.baosight.webapp.web;  
  2.   
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import org.springframework.stereotype.Controller;  
  13. import org.springframework.web.bind.annotation.RequestBody;  
  14. import org.springframework.web.bind.annotation.RequestMapping;  
  15. import org.springframework.web.bind.annotation.RequestMethod;  
  16. import org.springframework.web.bind.annotation.ResponseBody;  
  17. import org.springframework.web.multipart.MultipartFile;  
  18. import org.springframework.web.multipart.MultipartHttpServletRequest;  
  19. import org.springframework.web.multipart.commons.CommonsMultipartResolver;  
  20.   
  21. import com.baosight.webapp.bean.App;  
  22.   
  23.   
  24.   
  25. @Controller  
  26. @RequestMapping("/ngFileUpload")  
  27. public class NgFileUpload {  
  28.   
  29.     @RequestMapping(value="/test", method=RequestMethod.POST    )  
  30.     @ResponseBody  
  31.     public Map<String, Object> ngUpload(HttpServletRequest request,HttpServletResponse res){  
  32.         System.out.println("in");  
  33.         //接收参数  
  34.         int id= Integer.parseInt(request.getParameter("id"));  
  35.         System.out.println("id=="+id);  
  36.         Map<String, Object> resMap = new HashMap<String, Object>();  
  37.         resMap.put("result""error");  
  38.   
  39.         //解析器解析request的上下文  
  40.         CommonsMultipartResolver multipartResolver =  
  41.             new CommonsMultipartResolver(request.getSession().getServletContext());  
  42.           //先判断request中是否包涵multipart类型的数据,  
  43.         if(multipartResolver.isMultipart(request)){  
  44.            //再将request中的数据转化成multipart类型的数据  
  45.             MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;  
  46.             Iterator iter = multiRequest.getFileNames();  
  47.             while(iter.hasNext()){  
  48.                 //这里的name为fileItem的alias属性值,相当于form表单中name  
  49.                 String name=(String)iter.next();  
  50.                 System.out.println("name:"+name);  
  51.                 //根据name值拿取文件  
  52.                 MultipartFile file = multiRequest.getFile(name);  
  53.                 if(file != null){  
  54.                     String fileName = file.getOriginalFilename();  
  55.                     String path = "D:/test/" + fileName;  
  56.                     File localFile = new File(path);  
  57.                     if(!localFile.getParentFile().exists()) {  
  58.                          //如果目标文件所在的目录不存在,则创建父目录  
  59.                          localFile.getParentFile().mkdirs();  
  60.                          System.out.println("parent:"+localFile.getParentFile().getPath());  
  61.                      }  
  62.                     //写文件到本地  
  63.                     try {  
  64.                         file.transferTo(localFile);  
  65.                     } catch (IOException e) {  
  66.                         // TODO Auto-generated catch block  
  67.                         e.printStackTrace();  
  68.                         return resMap;  
  69.                     }  
  70.                 }  
  71.             }  
  72.           }else {  
  73.   
  74.               return resMap;  
  75.         }  
  76.           resMap.put("result""success");  
  77.           return resMap;  
  78.     }  
  79.   
  80.   
  81. }  

3.备注:

获取参数 request.getParameter("id")
这里的拿到的name为fileItem的alias属性值,相当于form表单中name           String name=(String)iter.next();
如果想通过file的name属性值获取文件,对文件进行分类接受,可以通过controller中item的alias属性值。

uploader.uploadAll()是提交所有文件的方法,但是实质上来说该方法是将每个文件提交一次。虽然只触发了一次uploadAll方法,但是有多少个文件就会进多少次后台。实质上是文件进行单个传输。

0 0