<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Android實現模擬表單上傳

      很久以前,寫過一篇關于下載的文章:基于HTTP協議的下載功能實現,今天對于Android上的文件上傳,也簡單的提兩筆。在Android上,一般使用Http 模擬表單或者FTP來進行文件上傳,使用FTP協議,可以直接使用Appache的FTPClient,使用方法很簡單,不再贅述。這里主要說明一下Http模擬表單上傳的實現。

      模擬表單上傳,其實也很簡單,主要需要在Http post 的數據體中構建表單信息(multipart/form),表單數據格式的規范,可以參考REC標準。下面是一個格式示例:

             ...
      Content-Type: multipart/form-data; boundary=
      ------WebKitFormBoundaryK7Ck1eEROPVUf1De
             Content-Length: 145000
      ...
      ------WebKitFormBoundaryK7Ck1eEROPVUf1De Content-Disposition: form-data; name="fileKey"; filename="bg_entry.png" Content-Type: image/png DATA OF FILE
             ------WebKitFormBoundaryK7Ck1eEROPVUf1De--
      
      

      表單請求重點在兩部分:

      Header

      1.通過Content-Type告知Server這是一個表單提交請求,并聲明自己使用的Boundary。Boundary相當于一個分隔符,用于標志表單數據的開始和結束。

      2.通過Content-Length告訴本次請求的數據長度,Post Body的長度(包括上傳文件長度)。

       

      Body:

      1.以Boundary分割表單數據。

      2.表單參數相當于簡單的Header,一般包括Content-Disposition(文件信息)和Content-Type(數據類型)兩個字段。

      3.各部分、各字段之間都要以CRLF分割。

      4.最后以Boundary加上“--”結束表單請求。

       

      核心代碼如下:

          protected String doUpload(HttpURLConnection connection, UploadParam param) throws Exception {
              String path = param.getPath();
              String fileKey = TextUtils.isEmpty(param.getFileKey()) ? "file" : param.getFileKey();
              String fileName = param.getFileName();
              String fileType = TextUtils.isEmpty(param.getContentType()) ? MIME_TYPE_ALL : param.getContentType();
      
              DataOutputStream outs = null;
              BufferedReader ins = null;
              FileInputStream fouts = null;
              String response = null;
              try {
                  //    Content-Disposition: form-data; name="fileKey"; filename="bg_entry.png"
                 //            Content-Type: image/png
                  StringBuilder builder = new StringBuilder(buildParams(param.getParams()));
                  builder.append(getBoundaryPrefixed())
                          .append(CRLF)
                          .append(String.format(HEADER_CONTENT_DISPOSITION + COLON_SPACE + FORM_DATA + SEMICOLON_SPACE + FILENAME, fileKey, fileName))
                          .append(CRLF)
                          .append(HEADER_CONTENT_TYPE).append(fileType)
                          .append(CRLF)
                          //Must jump to new line to indicate the beginning of data.
                          .append(CRLF);
                  byte[] headBuf = builder.toString().getBytes(CHARSET_UTF8);
                  //Must jump to new line to indicate the end of data.
                  byte[] tailBuf = (CRLF + getBoundaryPrefixed() + BOUNDARY_PREFIX + CRLF).getBytes(CHARSET_UTF8);
                  long currentBytes = 0;
                  File file = new File(path);
                  long totalSize = file.length() + headBuf.length + tailBuf.length;
                  //Generally speaking,Files larger than 4M should use streaming mode.
                  if (totalSize > 4 * 1024 * 1024) {
                      //Avoid oom when post large file.Ether way is ok.
                      connection.setChunkedStreamingMode(1024);
      //                connection.setFixedLengthStreamingMode(totalSize);
                  }
                  connection.setRequestProperty(HEADER_CONTENT_LENGTH, String.valueOf(totalSize));
                  connection.connect();
      
                  outs = new DataOutputStream(connection.getOutputStream());
                  outs.write(headBuf);
                  currentBytes += headBuf.length;
                  updateProgress(currentBytes, totalSize);
                  fouts = new FileInputStream(file);
                  byte[] buffer = new byte[1024];
                  int length = -1;
                  long startTime = System.currentTimeMillis();
                  long now = 0;
                  while ((length = fouts.read(buffer)) != -1) {
                      if (length > 0) {
                          outs.write(buffer, 0, length);
                          currentBytes += length;
                          now = System.currentTimeMillis();
                          if (now - startTime >= PROGRESS_RATE) {
                              updateProgress(currentBytes, totalSize);
                              startTime = now;
                          }
                      }
                      if (!canRun()) {
                          throw new Exception("Upload cancelled");
                      }
                  }
                  outs.write(tailBuf);
                  outs.flush();
                  updateProgress(totalSize, totalSize);
      
                  fouts.close();
                  fouts = null;
      
                  //Response.
                  if (connection.getResponseCode() != 200) {
                      throw new IllegalStateException(String.format("Error upload response: code:%s  msg:%s", connection.getResponseCode(), connection.getResponseMessage()));
                  }
                  ins = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                  String line;
                  StringBuffer b = new StringBuffer();
                  while ((line = ins.readLine()) != null) {
                      b.append(line);
                      if (!canRun()) {
                          throw new Exception("Upload cancelled");
                      }
                  }
      
                  response = b.toString();
                  if (TextUtils.isEmpty(response)) {
                      throw new NullPointerException("Null response: " + response);
                  }
                  outs.close();
                  outs = null;
                  ins.close();
                  ins = null;
              } finally {
                  if (fouts != null) {
                      fouts.close();
                      fouts = null;
                  }
                  if (outs != null) {
                      outs.close();
                      outs = null;
                  }
                  if (ins != null) {
                      ins.close();
                      ins = null;
                  }
              }
              return response;
          }

      主要步湊為:

      1.配置Header參數

      2.構建表單參數

      3.讀取和發送文件內容

      4.獲取響應碼

      其中值得注意的是,一般情況下,上傳會把所有的文件內容讀取到內存中再統一發送,如果文件過大,將可能導致內存溢出。所以在判斷文件內容大于4MB時,使用Chunked模式或Stream模式來避免OOM。

                  if (totalSize > 4 * 1024 * 1024) {
                      //Avoid oom when post large file.Ether way is ok.
                      connection.setChunkedStreamingMode(1024);
                      //connection.setFixedLengthStreamingMode(totalSize);
                  }

       

      更多代碼詳情請參考:TransferLibrary——一個Android文件傳輸庫,主要實現基于Http的文件上傳和下載,簡單方便,支持多任務下載,斷點續傳等等,歡迎小伙伴們使用交流:D

       

      posted @ 2017-07-16 10:29  Oxgen  Views(2440)  Comments(0)    收藏  舉報
      主站蜘蛛池模板: 麻豆国产高清精品国在线| 久久久久无码国产精品一区| 国产精品十八禁一区二区 | julia无码中文字幕一区| 欧美不卡无线在线一二三区观| 久久久国产成人一区二区| 成人无码视频97免费| 国产伦一区二区三区久久| 在线观看亚洲欧美日本| 巨熟乳波霸若妻在线播放| 亚洲精品久久久久成人2007| 性色av不卡一区二区三区| 茌平县| 伊人春色激情综合激情网| 郓城县| 国内精品久久久久影院网站| а∨天堂一区中文字幕| 国产伦码精品一区二区| 国产在线视频精品视频| 看亚洲黄色不在线网占| 午夜在线欧美蜜桃| 靖安县| 久久99精品久久久久久9| 野花香视频在线观看免费高清版| 无码人妻h动漫| 国产精品黄色片| 日韩av一区二区不卡在线| 亚洲精品天堂在线观看| 鲁一鲁一鲁一鲁一澡| 亚洲精品国产一二三区| 鹰潭市| 亚洲国产亚洲综合在线尤物| 做暖暖视频在线看片免费| 亚洲av成人在线一区| 无码全黄毛片免费看| 少妇午夜啪爽嗷嗷叫视频| 万宁市| 亚洲成人av在线资源网| 色爱综合另类图片av| 国产成人一区二区三区影院动漫| 奇米影视7777久久精品|