利用HttpURLConnection发送post请求上传多个文件

转自skyer_lei的专栏:http://blog.csdn.net/skyer_lei/article/details/6106709

本文要用java.net.HttpURLConnection来实现多个文件上传

1. 研究 form 表单到底封装了什么样的信息发送到servlet。

假如我参数写的内容是hello word,然后二个文件是二个简单的txt文件,form提交的信息为:

[xhtml]  view plain copy
  1. -----------------------------7da2e536604c8    
  2. Content-Disposition: form-data; name="username"    
  3.     
  4. hello word    
  5. -----------------------------7da2e536604c8    
  6. Content-Disposition: form-data; name="file1"filename="D:/haha.txt"    
  7. Content-Type: text/plain    
  8.     
  9. haha    
  10.   hahaha    
  11. -----------------------------7da2e536604c8    
  12. Content-Disposition: form-data; name="file2"filename="D:/huhu.txt"    
  13. Content-Type: text/plain    
  14.     
  15. messi     
  16. huhu    
  17. -----------------------------7da2e536604c8--    
 

 

研究下规律发现有如下几点特征

1.第一行是“ -----------------------------7d92221b604bc ”作为分隔符,然后是“ /r/n ” 回车换行符。 这个7d92221b604bc 分隔符浏览器是随机生成的。

2.第二行是Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt";name=对应input的name值,filename对应要上传的文件名(包括路径在内),

3.第三行如果是文件就有Content-Type: text/plain;这里上传的是txt文件所以是text/plain,如果上穿的是jpg图片的话就是image/jpg了,可以自己试试看看。

然后就是回车换行符。

4.在下就是文件或参数的内容或值了。如:hello word。

5.最后一行是-----------------------------7da2e536604c8--,注意最后多了二个--;

有了这些就可以使用HttpURLConnection来实现上传文件功能了

[java]  view plain copy
  1. private void upload(String[] uploadFiles, String actionUrl) {  
  2.       String end = "/r/n";  
  3.       String twoHyphens = "--";  
  4.       String boundary = "*****";  
  5.       try {  
  6.           URL url = new URL(actionUrl);  
  7.           HttpURLConnection con = (HttpURLConnection) url.openConnection();  
  8.            // 发送POST请求必须设置如下两行    
  9.           con.setDoInput(true);  
  10.           con.setDoOutput(true);  
  11.           con.setUseCaches(false);  
  12.           con.setRequestMethod("POST");  
  13.           con.setRequestProperty("Connection""Keep-Alive");  
  14.           con.setRequestProperty("Charset""UTF-8");  
  15.           con.setRequestProperty("Content-Type",  
  16.                   "multipart/form-data;boundary=" + boundary);  
  17.           DataOutputStream ds =  
  18.                   new DataOutputStream(con.getOutputStream());  
  19.           for (int i = 0; i < uploadFiles.length; i++) {  
  20.               String uploadFile = uploadFiles[i];  
  21.               String filename = uploadFile.substring(uploadFile.lastIndexOf("//") + 1);  
  22.               ds.writeBytes(twoHyphens + boundary + end);  
  23.               ds.writeBytes("Content-Disposition: form-data; " +  
  24.                       "name=/"file" + i + "/";filename=/"" +  
  25.                       filename + "/"" + end);  
  26.               ds.writeBytes(end);  
  27.               FileInputStream fStream = new FileInputStream(uploadFile);  
  28.               int bufferSize = 1024;  
  29.               byte[] buffer = new byte[bufferSize];  
  30.               int length = -1;  
  31.               while ((length = fStream.read(buffer)) != -1) {  
  32.                   ds.write(buffer, 0, length);  
  33.               }  
  34.               ds.writeBytes(end);  
  35.               /* close streams */  
  36.               fStream.close();  
  37.           }  
  38.           ds.writeBytes(twoHyphens + boundary + twoHyphens + end);  
  39.           ds.flush();  
  40.           // 定义BufferedReader输入流来读取URL的响应    
  41.           InputStream is = con.getInputStream();  
  42.           int ch;  
  43.           StringBuffer b = new StringBuffer();  
  44.           while ((ch = is.read()) != -1) {  
  45.               b.append((char) ch);  
  46.           }  
  47.           String s = b.toString();  
  48.           if (s.contains("successfully")) {  
  49.               // for (int i = 1; i < 5; i++) {  
  50.               int beginIndex = s.indexOf("url =") + 5;  
  51.               int endIndex = s.indexOf("/n", beginIndex);  
  52.               String urlStr = s.substring(beginIndex, endIndex).trim();  
  53.               System.out.println(urlStr);  
  54.               // }  
  55.           }  
  56.           ds.close();  
  57.       } catch (Exception e) {  
  58.       }  
  59.   }  
 

 

 至于接收文件上传的服务,以后再写

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值