歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android分別使用HTTP協議和TCP協議實現上傳文件

Android分別使用HTTP協議和TCP協議實現上傳文件

日期:2017/3/1 11:17:00   编辑:Linux編程

Android上傳文件有兩種方式,第一種是基於Http協議的HttpURLConnection,第二種是基於TCP協議的Socket。 這兩種方式的區別是使用HttpURLConnection上傳時內部有緩存機制,如果上傳較大文件會導致內存溢出。如果用TCP協議Socket方式上傳就會解決這種弊端。

HTTP協議HttpURLConnection

1. 通過URL封裝路徑打開一個HttpURLConnection

2.設置請求方式以及頭字段:Content-Type、Content-Length、Host

3.拼接數據發送

示例:

  1. private static final String BOUNDARY = "---------------------------7db1c523809b2";//數據分割線
  2. public boolean uploadHttpURLConnection(String username, String password, String path) throws Exception {
  3. //找到sdcard上的文件
  4. File file = new File(Environment.getExternalStorageDirectory(), path);
  5. //仿Http協議發送數據方式進行拼接
  6. StringBuilder sb = new StringBuilder();
  7. sb.append("--" + BOUNDARY + "\r\n");
  8. sb.append("Content-Disposition: form-data; name=\"username\"" + "\r\n");
  9. sb.append("\r\n");
  10. sb.append(username + "\r\n");
  11. sb.append("--" + BOUNDARY + "\r\n");
  12. sb.append("Content-Disposition: form-data; name=\"password\"" + "\r\n");
  13. sb.append("\r\n");
  14. sb.append(password + "\r\n");
  15. sb.append("--" + BOUNDARY + "\r\n");
  16. sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + path + "\"" + "\r\n");
  17. sb.append("Content-Type: image/pjpeg" + "\r\n");
  18. sb.append("\r\n");
  19. byte[] before = sb.toString().getBytes("UTF-8");
  20. byte[] after = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
  21. URL url = new URL("http://192.168.1.16:8080/14_Web/servlet/LoginServlet");
  22. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  23. conn.setRequestMethod("POST");
  24. conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
  25. conn.setRequestProperty("Content-Length", String.valueOf(before.length + file.length() + after.length));
  26. conn.setRequestProperty("HOST", "192.168.1.16:8080");
  27. conn.setDoOutput(true);
  28. OutputStream out = conn.getOutputStream();
  29. InputStream in = new FileInputStream(file);
  30. out.write(before);
  31. byte[] buf = new byte[1024];
  32. int len;
  33. while ((len = in.read(buf)) != -1)
  34. out.write(buf, 0, len);
  35. out.write(after);
  36. in.close();
  37. out.close();
  38. return conn.getResponseCode() == 200;
  39. }

TCP協議Socket

1.我們可以使用Socket發送TCP請求,將上傳數據分段發送

示例:

  1. public boolean uploadBySocket(String username, String password, String path) throws Exception {
  2. // 根據path找到SDCard中的文件
  3. File file = new File(Environment.getExternalStorageDirectory(), path);
  4. // 組裝表單字段和文件之前的數據
  5. StringBuilder sb = new StringBuilder();
  6. sb.append("--" + BOUNDARY + "\r\n");
  7. sb.append("Content-Disposition: form-data; name=\"username\"" + "\r\n");
  8. sb.append("\r\n");
  9. sb.append(username + "\r\n");
  10. sb.append("--" + BOUNDARY + "\r\n");
  11. sb.append("Content-Disposition: form-data; name=\"password\"" + "\r\n");
  12. sb.append("\r\n");
  13. sb.append(password + "\r\n");
  14. sb.append("--" + BOUNDARY + "\r\n");
  15. sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + path + "\"" + "\r\n");
  16. sb.append("Content-Type: image/pjpeg" + "\r\n");
  17. sb.append("\r\n");
  18. // 文件之前的數據
  19. byte[] before = sb.toString().getBytes("UTF-8");
  20. // 文件之後的數據
  21. byte[] after = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");
  22. URL url = new URL("http://192.168.1.199:8080/14_Web/servlet/LoginServlet");
  23. // 由於HttpURLConnection中會緩存數據, 上傳較大文件時會導致內存溢出, 所以我們使用Socket傳輸
  24. Socket socket = new Socket(url.getHost(), url.getPort());
  25. OutputStream out = socket.getOutputStream();
  26. PrintStream ps = new PrintStream(out, true, "UTF-8");
  27. // 寫出請求頭
  28. ps.println("POST /14_Web/servlet/LoginServlet HTTP/1.1");
  29. ps.println("Content-Type: multipart/form-data; boundary=" + BOUNDARY);
  30. ps.println("Content-Length: " + String.valueOf(before.length + file.length() + after.length));
  31. ps.println("Host: 192.168.1.199:8080");
  32. InputStream in = new FileInputStream(file);
  33. // 寫出數據
  34. out.write(before);
  35. byte[] buf = new byte[1024];
  36. int len;
  37. while ((len = in.read(buf)) != -1)
  38. out.write(buf, 0, len);
  39. out.write(after);
  40. in.close();
  41. out.close();
  42. return true;
  43. }
Copyright © Linux教程網 All Rights Reserved