Android項目——HttpUrlConnection上傳文件(圖片)


UI界面設計:

     

 

 

 

由於博客發布可能附加圖片,但是圖片(或者任何文件)信息必須放在http請求體的正文之中,這就需要我們使用HttpUrlConnection的時候構建Http正文。

我們先來看一下Http正文格式:

 1 POST /api/feed/ HTTP/1.1  
 2 Accept-Encoding: gzip  
 3 Content-Length: 225873  
 4 Content-Type: multipart/form-data; boundary=OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
 5 Host: www.myhost.com  
 6 Connection: Keep-Alive  
 7   
 8 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
 9 Content-Disposition: form-data; name="lng"  
10 Content-Type: text/plain; charset=UTF-8  
11 Content-Transfer-Encoding: 8bit  
12   
13 116.361545  
14 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
15 Content-Disposition: form-data; name="lat"  
16 Content-Type: text/plain; charset=UTF-8  
17 Content-Transfer-Encoding: 8bit  
18   
19 39.979006  
20 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
21 Content-Disposition: form-data; name="images"; filename="/storage/emulated/0/Camera/jdimage/1xh0e3yyfmpr2e35tdowbavrx.jpg"  
22 Content-Type: application/octet-stream  
23 Content-Transfer-Encoding: binary  
24   
25 這里是圖片的二進制數據  
26 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp--  

格式分析

請求頭分析

我們先看報文格式中的第一行:
POST /api/feed/ HTTP/1.1 

這一行就說明了這個請求的請求方式,即為POST方式,要請求的子路徑為/api/feed/,例如我們的服務器地址為www.myhost.com,然后我們的這個請求的完整路徑就是www.myhost.com/api/feed/,最后說明了HTTP協議的版本號為1.1。

Accept-Encoding: gzip  
Content-Length: 225873  
Content-Type: multipart/form-data; boundary=OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
Host: www.myhost.com  
Connection: Keep-Alive  
這幾個header的意思分別為服務器返回的數據需要使用gzip壓縮、請求的內容長度為225873、內容的類型為"multipart/form-data"、請求參數分隔符(boundary)為OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp、請求的根域名為www.myhost.com、HTTP連接方式為持久連接( Keep-Alive)。
 
其中這里需要注意的一點是分隔符,即boundary。boundary用於作為請求參數之間的界限標識,例如參數1和參數2之間需要有一個明確的界限,這樣服務器才能正確的解析到參數1和參數2。但是分隔符並不僅僅是boundary,而是下面這樣的格式:-- + boundary。例如這里的boundary為OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp,那么參數分隔符則為:

 

--OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp 
不管boundary本身有沒有這個"--",這個"--"都是不能省略的。

我們知道HTTP協議采用“請求-應答”模式,當使用普通模式,即非KeepAlive模式時,每個請求/應答客戶和服務器都要新建一個連接,完成之后立即斷開連接(HTTP協議為無連接的協議);當使用Keep-Alive模式(又稱持久連接、連接重用)時,Keep-Alive功能使客戶端到服務器端的連接持續有效,當出現對服務器的后續請求時,Keep-Alive功能避免了建立或者重新建立連接。

請求實體分析

請求實體其實就是HTTP POST請求的參數列表,每個參數以請求分隔符開始,即-- + boundary。例如下面這個參數。
1 --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp  
2 Content-Disposition: form-data; name="lng"  
3 Content-Type: text/plain; charset=UTF-8  
4 Content-Transfer-Encoding: 8bit  
5   
6 116.361545 
上面第一行為--OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp,也就是--加上boundary內容,最后加上一個換行 (這個換行不能省略),換行的字符串表示為"\r\n"。第二行為Content-Disposition和參數名,這里的參數名為lng,即經度。Content-Disposition就是當用戶想把請求所得的內容存為一個文件的時候提供一個默認的文件名,這里我們不過多關注。第三行為Content-Type,即WEB 服務器告訴瀏覽器自己響應的對象的類型,還有指定字符編碼為UTF-8。第四行是描述的是消息請求(request)和響應(response)所附帶的實體對象(entity)的傳輸形式,簡單文本數據我們設置為8bit,文件參數我們設置為binary就行。然后添加兩個換行之后才是參數的具體內容。例如這里的參數內容為116.361545。
注意這里的每行之間都是使用“\r\n”來換行的,最后一行和參數內容之間是兩個換行。文件參數也是一樣的格式,只是文件參數的內容是字節流。
這里要注意一下,普通文本參數和文件參數有如下兩個地方的不同,因為其內容本身的格式是不一樣的。
普通參數:
1 Content-Type: text/plain; charset=UTF-8  
2 Content-Transfer-Encoding: 8bit  

文件參數:

1 Content-Type: application/octet-stream  
2 Content-Transfer-Encoding: binary  

參數實體的最后一行是: --加上boundary加上--,最后換行,這里的 格式即為: --OCqxMF6-JxtxoMDHmoG5W5eY9MGRsTBp--。

 

具體代碼如下:

 1 public static void sendPostImg(String actionUrl, Map<String, File> files) throws IOException {
 2 
 3         String BOUNDARY = java.util.UUID.randomUUID().toString();   //利用系統工具類生成界限符
 4         String PREFIX = "--", LINEND = "\r\n";
 5         String MULTIPART_FROM_DATA = "multipart/form-data";
 6         String CHARSET = "UTF-8";
 7 
 8         URL uri = new URL(actionUrl);
 9         HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
10         conn.setReadTimeout(5 * 1000); // 緩存的最長時間
11         conn.setDoInput(true);// 允許輸入
12         conn.setDoOutput(true);// 允許輸出
13         conn.setUseCaches(false); // 不允許使用緩存
14         conn.setRequestMethod("POST");
15         conn.setRequestProperty("connection", "keep-alive");
16         conn.setRequestProperty("Charsert", "UTF-8");
17         conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
18 
19 //        // 首先組拼文本類型的參數
20 //        StringBuilder sb = new StringBuilder();
21 //        for (Map.Entry<String, String> entry : params.entrySet())
22 //        {
23 //            sb.append(PREFIX);
24 //            sb.append(BOUNDARY);
25 //            sb.append(LINEND);
26 //            sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
27 //            sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
28 //            sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
29 //            sb.append(LINEND);
30 //            sb.append(entry.getValue());
31 //            sb.append(LINEND);
32 //        }
33 
34         DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
35 //        outStream.write(sb.toString().getBytes());
36         InputStream in = null;
37         // 發送文件數據
38         if (files != null)
39         {
40             for (Map.Entry<String, File> file : files.entrySet())
41             {
42                 StringBuilder sb1 = new StringBuilder();
43                 sb1.append(PREFIX);
44                 sb1.append(BOUNDARY);
45                 sb1.append(LINEND);
46                 // name是post中傳參的鍵 filename是文件的名稱
47                 sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getValue().getName() + "\"" + LINEND);
48                 sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
49                 sb1.append("Content-Transfer-Encoding: binary"+LINEND);
50                 sb1.append(LINEND);
51                 outStream.write(sb1.toString().getBytes());
52                 Log.d("file",sb1.toString());
53                 InputStream is = new FileInputStream(file.getValue());
54                 byte[] buffer = new byte[1024];
55                 int len = 0;
56                 while ((len = is.read(buffer)) != -1)
57                 {
58                     outStream.write(buffer, 0, len);
59                 }
60 
61                 is.close();
62                 outStream.write(LINEND.getBytes());
63             }
64 
65             // 請求結束標志
66             byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
67             outStream.write(end_data);
68             outStream.flush();
69             // 得到響應碼
70             int res = conn.getResponseCode();
71             if (res == 200)
72             {
73                 in = conn.getInputStream();
74                 int ch;
75                 StringBuilder sb2 = new StringBuilder();
76                 while ((ch = in.read()) != -1)
77                 {
78                     sb2.append((char) ch);
79                 }
80                 Log.d(TAG,"狀態碼:"+res);
81             }else{
82                 Log.d(TAG,"狀態碼:"+res);
83             }
84             outStream.close();
85             conn.disconnect();
86         }
87         // return in.toString();
88     }

 

代碼是我參考其他博客改的,按照我自己的需求,將一些參數附加到URL后傳輸,Http正文只傳輸文件,因為我的參數決定了文件的命名格式,這樣后台不用遍歷查找參數后在遍歷一次尋找文件了,當然,如果你的參數比較重要的話還是將參數寫到Http正文中,這樣較為安全。

后台文件接收代碼:

 1 String temppath="C:/Program Files/apache-tomcat-9.0.31-windows-x64/apache-tomcat-9.0.31/tempfile";
 2         String path="C:/Program Files/apache-tomcat-9.0.31-windows-x64/apache-tomcat-9.0.31/webapps/STDEverything/images/blog";
 3         
 4         String userid=request.getParameter("userid");
 5         String blogid=request.getParameter("blogid");
 6         
 7         DiskFileItemFactory disk = new DiskFileItemFactory(1024*10,new File(temppath));
 8         ServletFileUpload up = new ServletFileUpload(disk);
 9         List<FileItem> list;
10         try {
11             list=up.parseRequest(request);
12             for(FileItem item:list) {
13                 if(!item.isFormField()) {
14                     InputStream inputStream=item.getInputStream();
15                     String filename=item.getName();
16                     String imgname=userid+"_"+DBUtil.getIdentifier()+"_"+filename;
17                     OutputStream outputStream=new FileOutputStream(path+"/"+imgname);
18                     System.out.println(imgname);
19                     int len=0;
20                     byte buff[]=new byte[1024];
21                     while((len=inputStream.read(buff))!=-1) {
22                         outputStream.write(buff,0,len);
23                     }
24                     outputStream.flush();
25                     outputStream.close();
26                     inputStream.close();
27                     DBUtil.writeBlogImg(request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/"))
28                             +"/images/blog/"+imgname, blogid);
29                 }else {
30                     System.out.println(item.getFieldName());
31                 }
32             }
33         } catch (FileUploadException e) {
34             // TODO Auto-generated catch block
35             e.printStackTrace();
36             response.getWriter().write("no");
37         }
38         response.getWriter().write("yes");

要想運行上文中的代碼接收文件,需要導入兩個jar包,

鏈接:http://pan.baidu.com/s/1jIbyn5s 密碼:84se

 

關於后台接收文件的學習可以參考這篇博客:

https://blog.csdn.net/linghuainian/article/details/82253247?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3

 

值得一提的是,當傳輸文件時,你的Http正文編碼格式必須改為"multipart/form-data",然而改為這種格式之后,普通的request.getParameter方式就無法獲取參數了,但可以獲取fileitem.InputStream來進行轉換,這也就是上文中說的為什么要遍歷獲取參數的原因。

 

至於Android項目的后期設想,因為博客圖片是和博客id進行綁定的,而且我存進去的是該圖片的網址,當后期顯示博文的具體內容時,可以先請求下來博客網址信息,用Android的Glide插件異步加載圖片。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM