原理
android客戶端模擬一個http的post請求到服務器端,服務器端接收相應的post請求后,返回響應信息給給客戶端。
背景
網上很多上傳到java服務器上的,找了好久,找到了上傳到php的了,思路跟我當初想的差不多,就是post過去。廢話不多說,直接上圖看代碼。
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服務器,希望本文分享能夠給大家帶來幫助。