Android上傳文件到Web服務(wù)器 PHP接收文件
Android上傳文件到服務(wù)器,通常采用構(gòu)造http協(xié)議的方法,模擬網(wǎng)頁P(yáng)OST方法傳輸文件,服務(wù)器端可以采用JavaServlet或者PHP來接收要傳輸?shù)奈募?。使用JavaServlet來接收文件的方法比較常見,在這里給大家介紹一個簡單的服務(wù)器端使用PHP語言來接收文件的例子。
服務(wù)器端代碼比較簡單,接收傳輸過來的文件:
<?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'];
}
?>
手機(jī)客戶端代碼:
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
{
// 要上傳的文件路徑,理論上可以傳輸任何文件,實(shí)際使用時根據(jù)需要處理
private String uploadFile = "/sdcard/testimg.jpg";
private String srcPath = "/sdcard/testimg.jpg";
// 服務(wù)器上接收文件的處理頁面,這里根據(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);
/* 設(shè)置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è)置每次傳輸?shù)牧鞔笮?,可以有效防止手機(jī)因?yàn)閮?nèi)存不足崩潰
// 此方法用于在預(yù)先不知道內(nèi)容長度時啟用沒有進(jìn)行內(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)絡(luò)訪問權(quán)限:
<uses-permission android:name="android.permission.INTERNET" />
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android App端與PHP Web端的簡單數(shù)據(jù)交互實(shí)現(xiàn)示例
- Android md5加密與php md5加密一致詳解
- Android通過PHP服務(wù)器實(shí)現(xiàn)登錄功能
- PHP簡單判斷iPhone、iPad、Android及PC設(shè)備的方法
- php、java、android、ios通用的3des方法(推薦)
- 基于PHP后臺的Android新聞瀏覽客戶端
- php生成Android客戶端掃描可登錄的二維碼
- Android異步上傳圖片到PHP服務(wù)器
- 使用PHP開發(fā)Android應(yīng)用程序技術(shù)介紹
- Android訪問php取回json數(shù)據(jù)實(shí)例
- android+json+php+mysql實(shí)現(xiàn)用戶反饋功能方法解析
- Android和PHP MYSQL交互開發(fā)實(shí)例
相關(guān)文章
Android編程實(shí)現(xiàn)的超炫圖片瀏覽器
這篇文章主要介紹了Android編程實(shí)現(xiàn)的超炫圖片瀏覽器,涉及Android針對圖片的查看與顯示方法,包含對圖片的各種常見操作技巧,需要的朋友可以參考下2015-12-12
Android 限制edittext 整數(shù)和小數(shù)位數(shù) 過濾器(詳解)
下面小編就為大家?guī)硪黄狝ndroid 限制edittext 整數(shù)和小數(shù)位數(shù) 過濾器(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Android制作一個錨點(diǎn)定位的ScrollView
這篇文章主要介紹了Android制作一個錨點(diǎn)定位的ScrollView,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
android實(shí)現(xiàn)ViewPager懶加載的三種方法
這篇文章主要介紹了android實(shí)現(xiàn)ViewPager懶加載的三種方法,懶加載在項目運(yùn)用中很廣泛,可以提高運(yùn)行速度,有興趣的可以了解一下。2017-03-03
Android中Listview點(diǎn)擊item不變顏色及設(shè)置listselector 無效的解決方案
這篇文章主要介紹了Android中Listview點(diǎn)擊item不變顏色及設(shè)置listselector 無效的原因及解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android編程之匿名內(nèi)部類與回調(diào)函數(shù)用法分析
這篇文章主要介紹了Android編程之匿名內(nèi)部類與回調(diào)函數(shù)用法,結(jié)合實(shí)例形式分析了Android編程中所涉及的java匿名內(nèi)部類與回調(diào)函數(shù)的概念、定義、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-10-10

