HttpURLConnection上传文件和字符串信息

  1. 以文件的形式传参  
  2. /** 
  3.      * 通过拼接的方式构造请求内容,实现参数传输以及文件传输 
  4.      *  
  5.      * @param actionUrl 访问的服务器URL 
  6.      * @param params 普通参数 
  7.      * @param files 文件参数 
  8.      * @return 
  9.      * @throws IOException 
  10.      */  
  11.     public static void post(String actionUrl, Map<String, String> params, Map<String, File> files) throws IOException  
  12.     {  
  13.   
  14.         String BOUNDARY = java.util.UUID.randomUUID().toString();  
  15.         String PREFIX = "--", LINEND = "\r\n";  
  16.         String MULTIPART_FROM_DATA = "multipart/form-data";  
  17.         String CHARSET = "UTF-8";  
  18.   
  19.         URL uri = new URL(actionUrl);  
  20.         HttpURLConnection conn = (HttpURLConnection) uri.openConnection();  
  21.         conn.setReadTimeout(5 * 1000); // 缓存的最长时间  
  22.         conn.setDoInput(true);// 允许输入  
  23.         conn.setDoOutput(true);// 允许输出  
  24.         conn.setUseCaches(false); // 不允许使用缓存  
  25.         conn.setRequestMethod("POST");  
  26.         conn.setRequestProperty("connection""keep-alive");  
  27.         conn.setRequestProperty("Charsert""UTF-8");  
  28.         conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);  
  29.   
  30.         // 首先组拼文本类型的参数  
  31.         StringBuilder sb = new StringBuilder();  
  32.         for (Map.Entry<String, String> entry : params.entrySet())  
  33.         {  
  34.             sb.append(PREFIX);  
  35.             sb.append(BOUNDARY);  
  36.             sb.append(LINEND);  
  37.             sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);  
  38.             sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);  
  39.             sb.append("Content-Transfer-Encoding: 8bit" + LINEND);  
  40.             sb.append(LINEND);  
  41.             sb.append(entry.getValue());  
  42.             sb.append(LINEND);  
  43.         }  
  44.   
  45.         DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());  
  46.         outStream.write(sb.toString().getBytes());  
  47.         InputStream in = null;  
  48.         // 发送文件数据  
  49.         if (files != null)  
  50.         {  
  51.             for (Map.Entry<String, File> file : files.entrySet())  
  52.             {  
  53.                 StringBuilder sb1 = new StringBuilder();  
  54.                 sb1.append(PREFIX);  
  55.                 sb1.append(BOUNDARY);  
  56.                 sb1.append(LINEND);  
  57.                 // name是post中传参的键 filename是文件的名称  
  58.                 sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getKey() + "\"" + LINEND);  
  59.                 sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);  
  60.                 sb1.append(LINEND);  
  61.                 outStream.write(sb1.toString().getBytes());  
  62.   
  63.                 InputStream is = new FileInputStream(file.getValue());  
  64.                 byte[] buffer = new byte[1024];  
  65.                 int len = 0;  
  66.                 while ((len = is.read(buffer)) != -1)  
  67.                 {  
  68.                     outStream.write(buffer, 0, len);  
  69.                 }  
  70.   
  71.                 is.close();  
  72.                 outStream.write(LINEND.getBytes());  
  73.             }  
  74.   
  75.             // 请求结束标志  
  76.             byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();  
  77.             outStream.write(end_data);  
  78.             outStream.flush();  
  79.             // 得到响应码  
  80.             int res = conn.getResponseCode();  
  81.             if (res == 200)  
  82.             {  
  83.                 in = conn.getInputStream();  
  84.                 int ch;  
  85.                 StringBuilder sb2 = new StringBuilder();  
  86.                 while ((ch = in.read()) != -1)  
  87.                 {  
  88.                     sb2.append((char) ch);  
  89.                 }  
  90.             }  
  91.             outStream.close();  
  92.             conn.disconnect();  
  93.         }  
  94.         // return in.toString();  
  95.     }  
  96.   
  97. 以数据流的形式传参  
  98. public static String postFile(String actionUrl, Map<String, String> params, Map<String, byte[]> files)  
  99.             throws Exception  
  100.     {  
  101.         StringBuilder sb2 = null;  
  102.         String BOUNDARY = java.util.UUID.randomUUID().toString();  
  103.         String PREFIX = "--", LINEND = "\r\n";  
  104.         String MULTIPART_FROM_DATA = "multipart/form-data";  
  105.         String CHARSET = "UTF-8";  
  106.   
  107.         URL uri = new URL(actionUrl);  
  108.         HttpURLConnection conn = (HttpURLConnection) uri.openConnection();  
  109.         conn.setReadTimeout(6 * 1000); // 缓存的最长时间  
  110.         conn.setDoInput(true);// 允许输入  
  111.         conn.setDoOutput(true);// 允许输出  
  112.         conn.setUseCaches(false); // 不允许使用缓存  
  113.         conn.setRequestMethod("POST");  
  114.         conn.setRequestProperty("connection""keep-alive");  
  115.         conn.setRequestProperty("Charsert""UTF-8");  
  116.         conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);  
  117.   
  118.         // 首先组拼文本类型的参数  
  119.         StringBuilder sb = new StringBuilder();  
  120.         for (Map.Entry<String, String> entry : params.entrySet())  
  121.         {  
  122.             sb.append(PREFIX);  
  123.             sb.append(BOUNDARY);  
  124.             sb.append(LINEND);  
  125.             sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);  
  126.             sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);  
  127.             sb.append("Content-Transfer-Encoding: 8bit" + LINEND);  
  128.             sb.append(LINEND);  
  129.             sb.append(entry.getValue());  
  130.             sb.append(LINEND);  
  131.         }  
  132.   
  133.         DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());  
  134.         outStream.write(sb.toString().getBytes());  
  135.         InputStream in = null;  
  136.         // 发送文件数据  
  137.         if (files != null)  
  138.         {  
  139.             for (Map.Entry<String, byte[]> file : files.entrySet())  
  140.             {  
  141.                 StringBuilder sb1 = new StringBuilder();  
  142.                 sb1.append(PREFIX);  
  143.                 sb1.append(BOUNDARY);  
  144.                 sb1.append(LINEND);  
  145.                 sb1.append("Content-Disposition: form-data; name=\"pic\"; filename=\"" + file.getKey() + "\"" + LINEND);  
  146.                 sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);  
  147.                 sb1.append(LINEND);  
  148.                 outStream.write(sb1.toString().getBytes());  
  149.   
  150.                 // InputStream is = new FileInputStream(file.getValue());  
  151.                 // byte[] buffer = new byte[1024];  
  152.                 // int len = 0;  
  153.                 // while ((len = is.read(buffer)) != -1)  
  154.                 // {  
  155.                 // outStream.write(buffer, 0, len);  
  156.                 // }  
  157.                 // is.close();  
  158.                 outStream.write(file.getValue());  
  159.   
  160.                 outStream.write(LINEND.getBytes());  
  161.             }  
  162.   
  163.             // 请求结束标志  
  164.             byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();  
  165.             outStream.write(end_data);  
  166.             outStream.flush();  
  167.             // 得到响应码  
  168.             int res = conn.getResponseCode();  
  169.             if (res == 200)  
  170.             {  
  171.                 in = conn.getInputStream();  
  172.                 int ch;  
  173.                 sb2 = new StringBuilder();  
  174.                 while ((ch = in.read()) != -1)  
  175.                 {  
  176.                     sb2.append((char) ch);  
  177.                 }  
  178.                 System.out.println(sb2.toString());  
  179.             }  
  180.             outStream.close();  
  181.             conn.disconnect();  
  182.             // 解析服务器返回来的数据  
  183.             return ParseJson.getEditMadIconResult(sb2.toString());  
  184.         }  
  185.         else  
  186.         {  
  187.             return "Update icon Fail";  
  188.         }  
  189.         // return in.toString();  
  190.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值