使用HttpURlConnection 發送POST請求上傳文件(帶參數)

前言

最近在做一個博客的小項目,需要用到文件上傳,HttpClient又被Android給棄用了,圖片框架暫時還沒學。只能使用HttpURLConnection來上傳。折騰了好久,今天終於順利地跟後臺完成了對接。因此,寫這篇博客梳理一下知識。

理論知識

背景

最早的HTTP POST是 不支持 文件上傳的,給編程開發帶來很多問題。但是在1995年,ietf出臺了rfc1867,也就是《RFC 1867 -Form-based File Upload in HTML》,用以支持文件上傳。所以Content-Type的類型擴充了multipart/form-data用以支持向服務器發送二進制數據。因此發送post請求時候,表單屬性enctype共有二個值可選,這個屬性管理的是表單的MIME編碼:

①application/x-www-form-urlencoded( 注:不設置enctype屬性時默認爲①)
②multipart/form-data


POST的報文請求分析

使用瀏覽器進行post請求將會發送以下數據:
 

//我是請求頭
POST /t2/upload.do HTTP/1.1
Accept-Charset: GBK,utf-8;
Connection: keep-alive
Content-Length: 60408
Content-Type:multipart/form-data; boundary=ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC //設置內容類型爲表單類型,同時定義了boundary “界限標識”
Host: w.sohu.com

 //這裏開始請求體的地盤啦,第一條請求體的實體數據(字符串參數)
--ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC       //這裏是"--"+boundary
Content-Disposition: form-data;name="xxx"   //name="xxx", xxx爲要發送的參數名
Content-Type: text/plain; charset=UTF-8     //設置內容類型爲text 編碼格式爲utf-8
Content-Transfer-Encoding: 8bit
 //這裏是一個空行(不可少)
116.361545        // 我勒個去(到這裏[空行之後的一行]才能寫上xxx的參數值)有點坑是吧,我也覺得
--ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC   //這一大串還是"--"+boundary
//第二條請求體的實體數據(圖片文件上傳)
Content-Disposition: form-data;name="pic"; filename="photo.jpg" //指定了文件
Content-Type: application/octet-stream          //設置了內容類型爲application/octet-stream  
Content-Transfer-Encoding: binary
 //還是一個空行(不可少)
[這裏是圖片二進制數據]
--ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC--

Boundary說明

根據RFC 1867定義,我們需要選擇一段數據作爲作爲請求參數之間的“界限標識” (即boundary屬性),這個“邊界數據”不能在內容其他地方出現,一般來說使用一段從概率上說“幾乎不可能”的數據即可。 
不同瀏覽器的實現不同 
火狐某次post的 boundary=---------------------------32404670520626, 
operade某次post的 boundary=----------E4SgDZXhJMgNE8jpwNdOAX
例如參數1和參數2之間需要有一個明確的界限,這樣服務器才能正確的解析到參數1和參數2。但是分隔符並不僅僅是boundary,而是下面這樣的格式:–+ boundary。 
如:boundary爲ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC,那麼參數分隔符則爲: 
--ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC 
不管boundary本身有沒有這個”--“(前綴),這個前綴都是不能省略的。 
最後--ZnGpDtePMx0KrHh_G0X99Yef9r8JZsRJSXC--爲結束標識

注:以上內容整理自

Multipart/form-data POST文件上傳詳解
HTTP POST請求報文格式分析與Java實現文件上傳


\r (回車) 與 \n (換行)

‘\r’ 回車,回到當前行的行首,而不會換到下一行,如果接着輸出的話,本行以前的內容會被逐一覆蓋;
‘\n’ 換行,換到當前位置的下一行,而不會回到行首; 
所以在寫完每一行數據之後要使用 \r\n才能達到切換至下一行行首的效果

實例

private static final int TIME_OUT = 8 * 1000;                          //超時時間
    private static final String CHARSET = "utf-8";                         //編碼格式
    private static final String PREFIX = "--";                            //前綴
    private static final String BOUNDARY = UUID.randomUUID().toString();  //邊界標識 隨機生成
    private static final String CONTENT_TYPE = "multipart/form-data";     //內容類型
    private static final String LINE_END = "\r\n";                        //換行
/**
     * post請求方法
     * */
    public static void postRequest(final Map<String, String> strParams, final Map<String, File> fileParams) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection conn = null;
                try {
                    URL url = new URL(requestUrl);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setReadTimeout(TIME_OUT);
                    conn.setConnectTimeout(TIME_OUT);
                    conn.setDoOutput(true);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);//Post 請求不能使用緩存   
                    //設置請求頭參數
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("Charset", "UTF-8");
                    conn.setRequestProperty("Content-Type", CONTENT_TYPE+";boundary=" + BOUNDARY);
                    /**
                     * 請求體
                     */
                    //上傳參數
                    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
                    //getStrParams()爲一個
                    dos.writeBytes( getStrParams(strParams).toString() );
                    dos.flush();

                    //文件上傳
                    StringBuilder fileSb = new StringBuilder();
                    for (Map.Entry<String, File> fileEntry: fileParams.entrySet()){
                        fileSb.append(PREFIX)
                                .append(BOUNDARY)
                                .append(LINE_END)
                                /**
                                 * 這裏重點注意: name裏面的值爲服務端需要的key 只有這個key 纔可以得到對應的文件
                                 * filename是文件的名字,包含後綴名的 比如:abc.png
                                 */
                                .append("Content-Disposition: form-data; name=\"file\"; filename=\""
                                        + fileEntry.getKey() + "\"" + LINE_END)
                                .append("Content-Type: image/jpg" + LINE_END) //此處的ContentType不同於 請求頭 中Content-Type
                                .append("Content-Transfer-Encoding: 8bit" + LINE_END)
                                .append(LINE_END);// 參數頭設置完以後需要兩個換行,然後纔是參數內容
                        dos.writeBytes(fileSb.toString());
                        dos.flush();
                        InputStream is = new FileInputStream(fileEntry.getValue());
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while ((len = is.read(buffer)) != -1){
                            dos.write(buffer,0,len);
                        }
                        is.close();
                        dos.writeBytes(LINE_END);
                    }
                    //請求結束標誌
                    dos.writeBytes(PREFIX + BOUNDARY + PREFIX + LINE_END);
                    dos.flush();
                    dos.close();
                    Log.e(TAG, "postResponseCode() = "+conn.getResponseCode() );
                    //讀取服務器返回信息
                    if (conn.getResponseCode() == 200) {
                        InputStream in = conn.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                        String line = null;
                        StringBuilder response = new StringBuilder();
                        while ((line = reader.readLine()) != null) {
                            response.append(line);
                        }
                        Log.e(TAG, "run: " + response);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if (conn!=null){
                        conn.disconnect();
                    }
                }
            }
        }).start();
    }

    /**
     * 對post參數進行編碼處理
     * */
    private static StringBuilder getStrParams(Map<String,String> strParams){
        StringBuilder strSb = new StringBuilder();
        for (Map.Entry<String, String> entry : strParams.entrySet() ){
            strSb.append(PREFIX)
                    .append(BOUNDARY)
                    .append(LINE_END)
                    .append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINE_END)
                    .append("Content-Type: text/plain; charset=" + CHARSET + LINE_END)
                    .append("Content-Transfer-Encoding: 8bit" + LINE_END)
                    .append(LINE_END)// 參數頭設置完以後需要兩個換行,然後纔是參數內容
                    .append(entry.getValue())
                    .append(LINE_END);
        }
        return strSb;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章