批量上传文件到服务器

批量上传文件到服务器

         利用MultipartFormDataInput实现。

           简单说明:MultipartFormDataInput会读取multipart/form-data类型数据过来的head和body,且以特定符号分割里面的内容,

                   利用测试工具fiddler 测试上传接口就会发现  选择需要上传的文档时候会以一定格式分割。

 

            比如我们上传两个文件到服务器上

 

      实现代码:

          

Java代码  
  1.       @POST   
  2. @Path ("/upload")  
  3. @Consumes("multipart/form-data")  
  4. @Produces("text/plain; charset=utf-8")  
  5. public synchronized String manyFileUpload(MultipartFormDataInput input) throws Exception {  
  6.      logger.info("开始上传多个文件---------");  
  7.       
  8.                 //获取head和body所有数据,返回一个list,上传单个文件的时候 直接get0 get1即可得到文件名和上传的内容  
  9.      logger.debug("读取文件头1:{}",input.getParts());  
  10.     // logger.debug("读取文件头2:{}",input.getFormData());  
  11.                 //获取获取head和body所有数据,返回一个map,可通过key来获取指定的head或body内容  
  12.      logger.debug("读取文件头3:{}",input.getFormDataMap());  
  13.      logger.debug("读取文件头4:{}",input.getPreamble());  
  14.       
  15.     Map<String, List<InputPart>> uploadForm = input.getFormDataMap();  
  16.     List<InputPart> bodys = uploadForm.get("Filedata");//得到上传文件的内容  
  17.     List<InputPart> heads = uploadForm.get("fileName");//得到上传文件的名称,主要获取后缀  
  18.   
  19.     //定义上传服务器的路径  
  20.     String webPath = "D:/ccc/cc/cc-ws/src/main/webapp/upload";  
  21.       
  22.     String fileNames = null;  
  23.       
  24.         //获取上传文件头和内容的总数,除以2就是要生成文件的个数(因为上传文件都是文件名和文件内容成对出现,如果多个附件一个文件名,传两遍文件名,  
  25.           上传服务器的保存的时候加上年月日时分秒,分辨附件,方便对应下载)  
  26.         for(int i=0;i<input.getParts().size()/2;i++){  
  27.             InputPart body = bodys.get(i);  
  28.             InputPart head = heads.get(i);  
  29.             //读取头,获取文件名  
  30.             InputStream is = head.getBody(InputStream.class,  
  31.                  null);  
  32.          //读取一遍名字  
  33.          ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  34.          int j = -1;  
  35.          while ((j = is.read()) != -1) {  
  36.              baos.write(j);  
  37.          }  
  38.                         //中文名字转码,根据需要转格式,也许是utf-8  
  39.          String fileName  = baos.toString("GBK");  
  40.            
  41.          //得到上传文件保存的路径  
  42.          String realFile = webPath + File.separator + fileName;  
  43.            
  44.          //读取内容,写入文件输出  
  45.          InputStream inputStream = body.getBody(InputStream.class,  
  46.                  null);  
  47.          File file = new File(realFile);  
  48.          FileUtils.copyInputStreamToFile(inputStream, file);  
  49.          logger.info("上传保存文件成功   返回生成的文件名:  {}  , 真实路径 :    {}", fileName,  
  50.                  realFile);  
  51.          if(fileNames == null)  
  52.              fileNames = fileName;  
  53.          else  
  54.              fileNames = fileNames+"||"+fileName;  
  55.         }  
  56.         //根据具体需求返回结果  
  57.          return fileNames;  
  58.           
  59. }  

 
           测试代码:

              采用html测试,即上图

Java代码  
  1. <html>    
  2.   <head>    
  3.     <base href="<%=basePath%>">    
  4.         
  5.     <title>My JSP 'fileupload.jsp' starting page</title>    
  6.         
  7.     <meta http-equiv="pragma" content="no-cache">    
  8.     <meta http-equiv="cache-control" content="no-cache">    
  9.     <meta http-equiv="expires" content="0">        
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    
  11.     <meta http-equiv="description" content="This is my page">    
  12.   
  13.   </head>    
  14.       
  15.   <body>    
  16.      <!-- enctype 默认是 application/x-www-form-urlencoded    action  对应接口地址  input标签中的name对应文件名与内容-->    
  17.      <form action="http://localhost:8080/cc/excel/upload"   
  18.               enctype="multipart/form-data" method="post">    
  19.             
  20.        <h4>批量上传 </h4>   
  21.                 注:文件名为上传附件的名字 <br/>    
  22.                文件名:<input type="text" name="fileName"> 附件:<input type="file"  name="Filedata"><br/>    
  23.                文件名:<input type="text" name="fileName"> 附件:<input type="file" name="Filedata"><br/>    
  24.               <input type="submit" value="提交"/>         
  25.      </form>    
  26.      
  27.   </body>    
  28. </html>   

转载于:https://my.oschina.net/pvpCC9IFwqz4/blog/413873

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值