激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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.zmynmublwnt.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服務器,希望本文分享能夠給大家帶來幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久久久久久久久高潮一区二区 | 亚洲午夜电影 | 国产一国产精品一级毛片 | 成人在线观看免费观看 | 香蕉秀 | 精品成人国产在线观看男人呻吟 | 高清成人在线 | 91九色丨porny丨国产jk | 香蕉视频网站在线观看 | 成人久久18免费 | 久久99网| 色蜜桃av | 亚欧在线免费观看 | 久久精品一区二区三区国产主播 | 羞羞答答影院 | 成人福利视频导航 | 99精品国产一区二区三区 | 国产一区二区三区视频观看 | 亚洲婷婷日日综合婷婷噜噜噜 | 中文字幕亚洲视频 | 国产一级毛片a | 精品国产视频一区二区三区 | 黄色片网站在线免费观看 | 精品一区二区久久久久久久网精 | 久章草影院 | 欧美成人免费香蕉 | 亚洲人成网站在e线播放 | 国产精品视频2021 | 神马久久精品综合 | 天天草天天爱 | 国产一区二区三区视频免费 | 中国老女人一级毛片视频 | 亚洲午夜不卡 | 久久老司机| 92精品国产自产在线 | 刘亦菲一区二区三区免费看 | 久国产 | 成人午夜视频在线观看免费 | 逼特逼视频在线观看 | 国产一精品久久99无吗一高潮 | 欧美日韩国产成人在线观看 |