一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android異步上傳圖片到PHP服務器

Android異步上傳圖片到PHP服務器

2021-04-26 17:59jerrylsxu Android

這篇文章主要介紹了Android異步上傳圖片到PHP服務器的相關資料,需要的朋友可以參考下

原理

android客戶端模擬一個http的post請求到服務器端,服務器端接收相應的post請求后,返回響應信息給給客戶端。

背景

網上很多上傳到java服務器上的,找了好久,找到了上傳到php的了,思路跟我當初想的差不多,就是post過去。廢話不多說,直接上圖看代碼。

Android異步上傳圖片到PHP服務器

Android異步上傳圖片到PHP服務器

Android異步上傳圖片到PHP服務器

php代碼

?
1
2
3
4
5
6
7
8
9
<?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'];
}
?>

android代碼

上傳的主要代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
private void uploadfile(string uploadurl)
{
string end = " ";
string twohyphens = "--";
string boundary = "******";
try
{
url url = new url(uploadurl);
httpurlconnection httpurlconnection = (httpurlconnection) url
.openconnection();//http連接
// 設置每次傳輸的流大小,可以有效防止手機因為內存不足崩潰
// 此方法用于在預先不知道內容長度時啟用沒有進行內部緩沖的 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);//post傳遞過去的編碼
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();//http輸入,即得到返回的結果
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());
}
}

因為安卓4.0之后耗時間的操作要求都在非ui線程中操作,即將前面的asynctask拿來用了吧~

asynctask傳送門:http://www.ythuaji.com.cn/article/159411.html

在這個類中,將上傳的操作放在doinbackground當中,可以有progressdialog顯示上傳了多少:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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);

還有就是,注意權限喲:

?
1
<uses-permission android:name="android.permission.internet" />

以上內容給大家介紹了android異步上傳圖片到php服務器,希望本文分享能夠給大家帶來幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 女人张开腿让男人桶爽 | 四虎影院com| 双夫1v2| 手机在线观看伦理片 | 果冻传媒在线完整免费观 | av中文字幕网免费观看 | 日本精品久久久久中文字幕 1 | 午夜影院在线免费观看 | 成人黄色a级片 | 国产男人搡女人免费视频 | 色综合久久中文字幕综合网 | 奇米网7777| www.俺去| 国产成人精品视频午夜 | 日本在线视频免费看 | 女人把私密部位张开让男人桶 | 校花在公车上被内射好舒服 | 日韩在线一区二区三区 | 国产精品第2页 | 亚洲国产精品成 | 55夜色66夜亚州精品站 | 日本红怡院亚洲红怡院最新 | ass天天裸妇pics | 99综合视频| 日韩欧美一区黑人vs日本人 | 欧美坐爱 | 91在线老师啪国自产 | 女人特黄大aaaaaa大片 | 免费福利资源站在线视频 | 999精品视频在线观看热6 | 色噜噜 男人的天堂在线观看 | 狠狠色婷婷丁香六月 | 亚洲精品国产成人中文 | 美女大鸡鸡 | 国产精品igao视频网网址 | 91色香sxmv最网页版新地址 | 乖女的嫩奶水h文孕妇 | 成人免费视频一区二区三区 | 教室里老师好紧h | 久久伊人在| 电车痴汉中文字幕 |