激情久久久_欧美视频区_成人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://m.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久久久久久久妲己 | 末成年女av片一区二区 | 狠狠操精品视频 | 亚洲国产成人久久成人52 | 国产91免费看 | 羞羞视频在线免费 | 97中文字幕第一一一页 | 国产91九色 | 蜜桃av鲁一鲁一鲁一鲁 | 999精品久久久 | 久久成人午夜视频 | 欧美色性 | 免费99热在线观看 | 在线播放免费播放av片 | 神马顶级推理片免费看 | 午夜在线观看视频网站 | 久久久99精品视频 | 黄色免费视频观看 | 久久17| 国产亚洲精品综合一区91 | 中文日韩| 在线看免电影网站 | h视频免费在线观看 | 九九精品视频观看 | 亚洲资源在线 | 天天看逼 | 欧美高清在线精品一区二区不卡 | 成人区精品一区二区婷婷 | 免费观看一级 | 免费黄色欧美视频 | 国产麻豆交换夫妇 | 色婷婷久久久亚洲一区二区三区 | 黄色免费在线网站 | 深夜视频在线 | 国产亚洲精品影达达兔 | 久久久久久久91 |