歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android上傳文件到Web服務器,PHP接收文件

Android上傳文件到Web服務器,PHP接收文件

日期:2017/3/1 10:29:51   编辑:Linux編程

Android上傳文件到服務器,通常采用構造http協議的方法,模擬網頁POST方法傳輸文件,服務器端可以采用JavaServlet或者PHP來接收要傳輸的文件。使用JavaServlet來接收文件的方法比較常見,在這裡給大家介紹一個簡單的服務器端使用PHP語言來接收文件的例子。

服務器端代碼比較簡單,接收傳輸過來的文件:

  1. <?php
  2. $target_path = "./upload/";//接收文件目錄
  3. $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
  4. if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
  5. echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
  6. } else{
  7. echo "There was an error uploading the file, please try again!" . $_FILES['uploadedfile']['error'];
  8. }
  9. ?>

手機客戶端代碼:

  1. package com.figo.uploadfile;
  2. import java.io.BufferedReader;
  3. import java.io.DataOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import android.app.Activity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. public class UploadfileActivity extends Activity
  16. {
  17. // 要上傳的文件路徑,理論上可以傳輸任何文件,實際使用時根據需要處理
  18. private String uploadFile = "/sdcard/testimg.jpg";
  19. private String srcPath = "/sdcard/testimg.jpg";
  20. // 服務器上接收文件的處理頁面,這裡根據需要換成自己的
  21. private String actionUrl = "http://10.100.1.208/receive_file.php";
  22. private TextView mText1;
  23. private TextView mText2;
  24. private Button mButton;
  25. @Override
  26. public void onCreate(Bundle savedInstanceState)
  27. {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. mText1 = (TextView) findViewById(R.id.myText2);
  31. mText1.setText("文件路徑:\n" + uploadFile);
  32. mText2 = (TextView) findViewById(R.id.myText3);
  33. mText2.setText("上傳網址:\n" + actionUrl);
  34. /* 設置mButton的onClick事件處理 */
  35. mButton = (Button) findViewById(R.id.myButton);
  36. mButton.setOnClickListener(new View.OnClickListener()
  37. {
  38. @Override
  39. public void onClick(View v)
  40. {
  41. uploadFile(actionUrl);
  42. }
  43. });
  44. }
  45. /* 上傳文件至Server,uploadUrl:接收文件的處理頁面 */
  46. private void uploadFile(String uploadUrl)
  47. {
  48. String end = "\r\n";
  49. String twoHyphens = "--";
  50. String boundary = "******";
  51. try
  52. {
  53. URL url = new URL(uploadUrl);
  54. HttpURLConnection httpURLConnection = (HttpURLConnection) url
  55. .openConnection();
  56. // 設置每次傳輸的流大小,可以有效防止手機因為內存不足崩潰
  57. // 此方法用於在預先不知道內容長度時啟用沒有進行內部緩沖的 HTTP 請求正文的流。
  58. httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
  59. // 允許輸入輸出流
  60. httpURLConnection.setDoInput(true);
  61. httpURLConnection.setDoOutput(true);
  62. httpURLConnection.setUseCaches(false);
  63. // 使用POST方法
  64. httpURLConnection.setRequestMethod("POST");
  65. httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
  66. httpURLConnection.setRequestProperty("Charset", "UTF-8");
  67. httpURLConnection.setRequestProperty("Content-Type",
  68. "multipart/form-data;boundary=" + boundary);
  69. DataOutputStream dos = new DataOutputStream(
  70. httpURLConnection.getOutputStream());
  71. dos.writeBytes(twoHyphens + boundary + end);
  72. dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
  73. + srcPath.substring(srcPath.lastIndexOf("/") + 1)
  74. + "\""
  75. + end);
  76. dos.writeBytes(end);
  77. FileInputStream fis = new FileInputStream(srcPath);
  78. byte[] buffer = new byte[8192]; // 8k
  79. int count = 0;
  80. // 讀取文件
  81. while ((count = fis.read(buffer)) != -1)
  82. {
  83. dos.write(buffer, 0, count);
  84. }
  85. fis.close();
  86. dos.writeBytes(end);
  87. dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
  88. dos.flush();
  89. InputStream is = httpURLConnection.getInputStream();
  90. InputStreamReader isr = new InputStreamReader(is, "utf-8");
  91. BufferedReader br = new BufferedReader(isr);
  92. String result = br.readLine();
  93. Toast.makeText(this, result, Toast.LENGTH_LONG).show();
  94. dos.close();
  95. is.close();
  96. } catch (Exception e)
  97. {
  98. e.printStackTrace();
  99. setTitle(e.getMessage());
  100. }
  101. }
  102. }

在AndroidManifest.xml文件裡添加網絡訪問權限:

  1. <uses-permission android:name="android.permission.INTERNET" /

運行結果:

Copyright © Linux教程網 All Rights Reserved