Android上傳文件到Web服務器,PHP接收文件
Android上傳文件到服務器,通常采用構造http協議的方法,模擬網頁POST方法傳輸文件,服務器端可以采用JavaServlet或者PHP來 接收要傳輸的文件。使用JavaServlet來接收文件的方法比較常見,在這里給大家介紹一個簡單的服務器端使用PHP語言來接收文件的例子。
服務器端代碼比較簡單,接收傳輸過來的文件:
<?php $target_path = "./upload/";//接收文件目錄 $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!" . $_FILES['uploadedfile']['error']; } ?>手機客戶端代碼:
package com.figo.uploadfile; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class UploadfileActivity extends Activity { // 要上傳的文件路徑,理論上可以傳輸任何文件,實際使用時根據需要處理 private String uploadFile = "/sdcard/testimg.jpg"; private String srcPath = "/sdcard/testimg.jpg"; // 服務器上接收文件的處理頁面,這里根據需要換成自己的 private String actionUrl = "http://10.100.1.208/receive_file.php"; private TextView mText1; private TextView mText2; private Button mButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mText1 = (TextView) findViewById(R.id.myText2); mText1.setText("文件路徑:\n" + uploadFile); mText2 = (TextView) findViewById(R.id.myText3); mText2.setText("上傳網址:\n" + actionUrl); /* 設置mButton的onClick事件處理 */ mButton = (Button) findViewById(R.id.myButton); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { uploadFile(actionUrl); } }); } /* 上傳文件至Server,uploadUrl:接收文件的處理頁面 */ private void uploadFile(String uploadUrl) { String end = "\r\n"; String twoHyphens = "--"; String boundary = "******"; try { URL url = new URL(uploadUrl); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); // 設置每次傳輸的流大小,可以有效防止手機因為內存不足崩潰 // 此方法用于在預先不知道內容長度時啟用沒有進行內部緩沖的 HTTP 請求正文的流。 httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K // 允許輸入輸出流 httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); // 使用POST方法 httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream( httpURLConnection.getOutputStream()); dos.writeBytes(twoHyphens + boundary + end); dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + srcPath.substring(srcPath.lastIndexOf("/") + 1) + "\"" + end); dos.writeBytes(end); FileInputStream fis = new FileInputStream(srcPath); byte[] buffer = new byte[8192]; // 8k int count = 0; // 讀取文件 while ((count = fis.read(buffer)) != -1) { dos.write(buffer, 0, count); } fis.close(); dos.writeBytes(end); dos.writeBytes(twoHyphens + boundary + twoHyphens + end); dos.flush(); InputStream is = httpURLConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String result = br.readLine(); Toast.makeText(this, result, Toast.LENGTH_LONG).show(); dos.close(); is.close(); } catch (Exception e) { e.printStackTrace(); setTitle(e.getMessage()); } } }在AndroidManifest.xml文件里添加網絡訪問權限:
<uses-permission android:name="android.permission.INTERNET" />
運行結果:
以上已經能夠實現文件上傳,但沒有上傳進度。這次在之前的基礎上添加進度顯示,Java代碼如下所示:
package com.lenovo.uptest; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class UploadtestActivity extends Activity { /** Called when the activity is first created. */ /** * Upload file to web server with progress status, client: android; * server:php * **/ private TextView mtv1 = null; private TextView mtv2 = null; private Button bupload = null; private String uploadFile = "/sdcard/testimg.jpg"; private String actionUrl = "http://10.100.1.208/receive_file.php"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mtv1 = (TextView) findViewById(R.id.mtv1); mtv1.setText("文件路徑:\n" + uploadFile); mtv2 = (TextView) findViewById(R.id.mtv2); mtv2.setText("上傳地址:\n" + actionUrl); bupload = (Button) findViewById(R.id.bupload); bupload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub FileUploadTask fileuploadtask = new FileUploadTask(); fileuploadtask.execute(); } }); } // show Dialog method private void showDialog(String mess) { new AlertDialog.Builder(UploadtestActivity.this).setTitle("Message") .setMessage(mess) .setNegativeButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); } class FileUploadTask extends AsyncTask<Object, Integer, Void> { private ProgressDialog dialog = null; HttpURLConnection connection = null; DataOutputStream outputStream = null; DataInputStream inputStream = null; //the file path to upload String pathToOurFile = "/sdcard/testimg.jpg"; //the server address to process uploaded file String urlServer = "http://10.100.1.208/receive_file.php"; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; File uploadFile = new File(pathToOurFile); long totalSize = uploadFile.length(); // Get size of file, bytes @Override protected void onPreExecute() { dialog = new ProgressDialog(UploadtestActivity.this); dialog.setMessage("正在上傳..."); dialog.setIndeterminate(false); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(0); dialog.show(); } @Override protected Void doInBackground(Object... arg0) { long length = 0; int progress; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 256 * 1024;// 256KB try { FileInputStream fileInputStream = new FileInputStream(new File( pathToOurFile)); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Set size of every block for post connection.setChunkedStreamingMode(256 * 1024);// 256KB // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Charset", "UTF-8"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); outputStream = new DataOutputStream( connection.getOutputStream()); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outputStream.write(buffer, 0, bufferSize); length += bufferSize; progress = (int) ((length * 100) / totalSize); publishProgress(progress); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); publishProgress(100); // Responses from the server (code and message) int serverResponseCode = connection.getResponseCode(); String serverResponseMessage = connection.getResponseMessage(); /* 將Response顯示于Dialog */ // Toast toast = Toast.makeText(UploadtestActivity.this, "" // + serverResponseMessage.toString().trim(), // Toast.LENGTH_LONG); // showDialog(serverResponseMessage.toString().trim()); /* 取得Response內容 */ // InputStream is = connection.getInputStream(); // int ch; // StringBuffer sbf = new StringBuffer(); // while ((ch = is.read()) != -1) { // sbf.append((char) ch); // } // // showDialog(sbf.toString().trim()); fileInputStream.close(); outputStream.flush(); outputStream.close(); } catch (Exception ex) { // Exception handling // showDialog("" + ex); // Toast toast = Toast.makeText(UploadtestActivity.this, "" + // ex, // Toast.LENGTH_LONG); } return null; } @Override protected void onProgressUpdate(Integer... progress) { dialog.setProgress(progress[0]); } @Override protected void onPostExecute(Void result) { try { dialog.dismiss(); // TODO Auto-generated method stub } catch (Exception e) { } } } }
服務器端仍然和之前的一樣。
這里使用了AsyncTask,它使創建需要與用戶界面交互的長時間運行的任務變得更簡單,適用于簡單的異步處理,不需要借助線程和Handler即可實現。
AsyncTask是抽象類.AsyncTask定義了三種泛型類型 Params,Progress和Result。
Params 啟動任務執行的輸入參數,比如HTTP請求的URL。
Progress 后臺任務執行的百分比。
Result 后臺執行任務最終返回的結果,比如String。
AsyncTask的執行分為四個步驟,每一步都對應一個回調方法,這些方法不應該由應用程序調用,開發者需要做的就是實現這些方法。
1) 子類化AsyncTask
2) 實現AsyncTask中定義的下面一個或幾個方法
onPreExecute(), 該方法將在執行實際的后臺操作前被UI thread調用。可以在該方法中做一些準備工作,如在界面上顯示一個進度條。
doInBackground(Params...), 將在onPreExecute 方法執行后馬上執行,該方法運行在后臺線程中。這里將主要負責執行那些很耗時的后臺計算工作。可以調用 publishProgress方法來更新實時的任務進度。該方法是抽象方法,子類必須實現。
onProgressUpdate(Progress...),在publishProgress方法被調用后,UI thread將調用這個方法從而在界面上展示任務的進展情況,例如通過一個進度條進行展示。
onPostExecute(Result), 在doInBackground 執行完成后,onPostExecute 方法將被UI thread調用,后臺的計算結果將通過該方法傳遞到UI thread.
為了正確的使用AsyncTask類,以下是幾條必須遵守的準則:
1) Task的實例必須在UI thread中創建
2) execute方法必須在UI thread中調用
3) 不要手動的調用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)這幾個方法
4) 該task只能被執行一次,否則多次調用時將會出現異常
doInBackground方法和onPostExecute的參數必須對應,這兩個參數在AsyncTask聲明的泛型參數列表中指定,第一個為 doInBackground接受的參數,第二個為顯示進度的參數,第三個為doInBackground返回和onPostExecute傳入的參數。
運行結果如下:
轉自:http://blog.csdn.net/sxwyf248/article/details/7012758