php服務器php
<?php $target_path = "./upload1/";//接收文件目錄 $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']; } ?>
2.android客戶端java
總共4步:設置http請求:http頭---http正文---發送http請求---返回服務器結果android
待解決:只能上傳小文件。文件名中不能有中文。緩存
package com.tianlei.httpUrlConnection_PHPUpload; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class Uploadfile{ // 要上傳的文件路徑,理論上能夠傳輸任何文件,實際使用時根據須要處理 //private String uploadFile = "/sdcard/testimg.jpg"; //private String srcPath = "/sdcard/testimg.jpg"; private String srcPath; // 服務器上接收文件的處理頁面,這裏根據須要換成本身的 private String actionUrl = "http://120.126.16.52/uploadfile.php"; private Context c; public Uploadfile(Context c, String filepath){ this.c = c; this.srcPath = filepath; } /* 上傳文件至Server,uploadUrl:接收文件的處理頁面 */ public void uploadFile(){ String uploadUrl = actionUrl; String end = "\r\n"; String twoHyphens = "--"; //每一個字段用「--」分隔 String boundary = "******";//在HTTP請求頭設置一個分隔符 try{ URL url = new URL(uploadUrl);//創建url /*打開url鏈接 * 此處的urlConnection對象其實是根據URL的 請求協議(此處是http)生成的URLConnection類 * 的子類HttpURLConnection,故此處最好將其轉化爲HttpURLConnection類型的對象, * 以便用到HttpURLConnection更多的API*/ HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); /*http頭 * 設置每次傳輸的流大小,能夠有效防止手機由於內存不足崩潰,此方法用於在預先不知道內容長度時啓用, * 沒有進行內部緩衝的 HTTP 請求正文的流。*/ //httpURLConnection.setChunkedStreamingMode(256 * 1024);// 256K httpURLConnection.setConnectTimeout(10 * 60 * 1000); // 容許輸入輸出流 httpURLConnection.setDoInput(true);// 設置是否從httpUrlConnection讀入,默認狀況下是true /*設置是否向httpUrlConnection輸出,由於這個是post請求,參數要放在 http正文內,所以須要設爲true, 默認狀況下是false*/ httpURLConnection.setDoOutput(true); // Post 請求不能使用緩存 httpURLConnection.setUseCaches(false);//沒有進行內部緩衝 // 設定請求的方法爲"POST",默認是GET httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charset", "UTF-8"); //首先在HTTP請求頭設置一個分隔符***** httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); //httpURLConnection.connect(); // 鏈接 /*http請求的正文 * 正文的內容是經過outputStream流寫入的 * 此處getOutputStream會隱含的進行connect(即:如同調用上面的connect()方法, 因此在開發中不調用上述的connect()也能夠)。*/ DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream()); //每一個字段用「--」分隔 dos.writeBytes(twoHyphens + boundary + end); /*srcPath.substring(srcPath.lastIndexOf("/") + 1):表示文件名字 */ 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(); //發送請求 // 在調用下邊的getInputStream()函數時纔將內存緩衝區中封裝好的完整的HTTP請求電文發送到服務端。 InputStream is = httpURLConnection.getInputStream(); //至此,http請求已經被髮送到服務器 InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); //BufferedReader br = new BufferedReader(new InputStreamReader // (httpURLConnection.getInputStream(),"utf-8")); /*獲取返回結果. * 在getInputStream()函數調用的時候,就會把準備好的http請求 正式發送到服務器了, * 而後返回一個輸入流,用於讀取服務器對於這次http請求的返回信息。*/ String result = br.readLine(); Toast.makeText(c, result, Toast.LENGTH_SHORT).show(); dos.close(); is.close(); }catch (Exception e){ e.printStackTrace(); //setTitle(e.getMessage()); Toast.makeText(c, e.getMessage(), Toast.LENGTH_SHORT).show(); } } }