最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年03月30日 11:51:42   作者:chaoyu168  
這篇文章主要為大家詳細介紹了Android上傳文件到Web服務器,PHP接收文件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android上傳文件到服務器,通常采用構(gòu)造http協(xié)議的方法,模擬網(wǎng)頁POST方法傳輸文件,服務器端可以采用JavaServlet或者PHP來接收要傳輸?shù)奈募?。使用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 
{ 
 // 要上傳的文件路徑,理論上可以傳輸任何文件,實際使用時根據(jù)需要處理 
 private String uploadFile = "/sdcard/testimg.jpg"; 
 private String srcPath = "/sdcard/testimg.jpg"; 
 // 服務器上接收文件的處理頁面,這里根據(jù)需要換成自己的 
 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("上傳網(wǎng)址:\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(); 
   // 設置每次傳輸?shù)牧鞔笮?,可以有效防止手機因為內(nèi)存不足崩潰 
   // 此方法用于在預先不知道內(nèi)容長度時啟用沒有進行內(nèi)部緩沖的 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文件里添加網(wǎng)絡訪問權(quán)限:
<uses-permission android:name="android.permission.INTERNET" /> 

運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

孝感市| 西安市| 汝阳县| 邯郸市| 镇赉县| 且末县| 曲阜市| 乡城县| 孙吴县| 乃东县| 沛县| 南雄市| 鹤壁市| 化州市| 黔西县| 临漳县| 珲春市| 长阳| 通化县| 上犹县| 柳河县| 太湖县| 建始县| 瑞金市| 海阳市| 麦盖提县| 平山县| 石林| 高邮市| 张家口市| 香河县| 临江市| 凤冈县| 寻乌县| 班玛县| 祥云县| 宜章县| 罗江县| 临清市| 盐边县| 晋城|