android 使用post方式上传文件

来源:互联网 发布:sky黑历史知乎 编辑:程序博客网 时间:2024/06/05 22:52

转载自:http://blog.csdn.net/banyingli/article/details/6820436

[java] view plaincopy
  1. public static String post(String actionUrl, Map<String, String> params,  
  2.         Map<String, File> files) throws IOException {  
  3.   
  4.     String BOUNDARY = java.util.UUID.randomUUID().toString();  
  5.     String PREFIX = "--", LINEND = "\r\n";  
  6.     String MULTIPART_FROM_DATA = "multipart/form-data";  
  7.     String CHARSET = "UTF-8";  
  8.     URL uri = new URL(actionUrl);  
  9.     HttpURLConnection conn = (HttpURLConnection) uri.openConnection();  
  10.     conn.setReadTimeout(5 * 1000);  
  11.     conn.setDoInput(true);// 允许输入  
  12.     conn.setDoOutput(true);// 允许输出  
  13.     conn.setUseCaches(false);  
  14.     conn.setRequestMethod("POST"); // Post方式  
  15.     conn.setRequestProperty("connection""keep-alive");  
  16.     conn.setRequestProperty("Charsert""UTF-8");  
  17.     conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA  
  18.             + ";boundary=" + BOUNDARY);  
  19.   
  20.     // 首先组拼文本类型的参数  
  21.     StringBuilder sb = new StringBuilder();  
  22.     for (Map.Entry<String, String> entry : params.entrySet()) {  
  23.         sb.append(PREFIX);  
  24.         sb.append(BOUNDARY);  
  25.         sb.append(LINEND);  
  26.         sb.append("Content-Disposition: form-data; name=\""  
  27.                 + entry.getKey() + "\"" + LINEND);  
  28.         sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);  
  29.         sb.append("Content-Transfer-Encoding: 8bit" + LINEND);  
  30.         sb.append(LINEND);  
  31.         sb.append(entry.getValue());  
  32.         sb.append(LINEND);  
  33.     }  
  34.   
  35.     DataOutputStream outStream = new DataOutputStream(conn  
  36.             .getOutputStream());  
  37.     outStream.write(sb.toString().getBytes());  
  38.   
  39.     // 发送文件数据  
  40.     if (files != null)  
  41.         for (Map.Entry<String, File> file : files.entrySet()) {  
  42.             StringBuilder sb1 = new StringBuilder();  
  43.             sb1.append(PREFIX);  
  44.             sb1.append(BOUNDARY);  
  45.             sb1.append(LINEND);  
  46.             sb1  
  47.                     .append("Content-Disposition: form-data; name=\"file\"; filename=\""  
  48.                             + file.getKey() + "\"" + LINEND);  
  49.             sb1.append("Content-Type: application/octet-stream; charset="  
  50.                     + CHARSET + LINEND);  
  51.             sb1.append(LINEND);  
  52.             outStream.write(sb1.toString().getBytes());  
  53.             InputStream is = new FileInputStream(file.getValue());  
  54.             byte[] buffer = new byte[1024];  
  55.             int len = 0;  
  56.             while ((len = is.read(buffer)) != -1) {  
  57.                 outStream.write(buffer, 0, len);  
  58.             }  
  59.   
  60.             is.close();  
  61.             outStream.write(LINEND.getBytes());  
  62.         }  
  63.   
  64.     // 请求结束标志  
  65.     byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();  
  66.     outStream.write(end_data);  
  67.     outStream.flush();  
  68.   
  69.     // 得到响应码  
  70.     int res = conn.getResponseCode();  
  71.     InputStream in = conn.getInputStream();  
  72.     InputStreamReader isReader = new InputStreamReader(in);  
  73.     BufferedReader bufReader = new BufferedReader(isReader);  
  74.     String line = null;  
  75.     String data = "OK";  
  76.   
  77.     while ((line = bufReader.readLine()) != null)  
  78.         data += line;  
  79.   
  80.     if (res == 200) {  
  81.         int ch;  
  82.         StringBuilder sb2 = new StringBuilder();  
  83.         while ((ch = in.read()) != -1) {  
  84.             sb2.append((char) ch);  
  85.         }  
  86.     }  
  87.     outStream.close();  
  88.     conn.disconnect();  
  89.     return in.toString();  
  90. }  

0 0
原创粉丝点击