2017-08-03 56 views
7

我完全陌生於android開發,並且我一直通過android中的httpurlconnection發送大文件。 我用於通過HttpURLConnection的上傳文件的代碼是如何在Android上暫停Httpurlconnection按鈕單擊

try { 
      FileInputStream fileInputStream = new FileInputStream(new File(
        pathToOurFile)); 

      URL url = new URL(urlServer); 
      connection = (HttpURLConnection) url.openConnection(); 

      // Allow Inputs & Outputs 
      connection.setDoInput(true); 
      connection.setDoOutput(true); 
      connection.setUseCaches(false); 
      connection.setChunkedStreamingMode(1024) 

      // Enable POST method 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Connection", "Keep-Alive"); 
      connection.setRequestProperty("Content-Type", 
        "multipart/form-data; boundary=" + boundary); 

      outputStream = new DataOutputStream(connection.getOutputStream()); 
      outputStream.writeBytes(twoHyphens + boundary + lineEnd); 


      // TOKENID 
      String token = "anyvalye"; 
      outputStream.writeBytes("Content-Disposition: form-data; name=\"Token\"" + lineEnd); 
      outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd); 
      outputStream.writeBytes("Content-Length: " + token.length() + lineEnd); 
      outputStream.writeBytes(lineEnd); 
      outputStream.writeBytes(token + lineEnd); 
      outputStream.writeBytes(twoHyphens + boundary + lineEnd); 

      String connstr = null; 
      connstr = "Content-Disposition: form-data; name=\"UploadFile\";filename=\"" 
        + pathToOurFile + "\"" + lineEnd; 

      outputStream.writeBytes(connstr); 
      outputStream.writeBytes(lineEnd); 

      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      TotalFileSize=bufferSize; 

      System.out.println("Initial Max Size" + bytesAvailable + ""); 
      buffer = new byte[bufferSize]; 

      // Read file 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      try { 
       while (bytesRead > 0) { 
        try { 
         outputStream.write(buffer, 0, bufferSize); 
        } catch (OutOfMemoryError e) { 
         e.printStackTrace(); 
         response = "outofmemoryerror"; 
         return response; 
        } 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       response = "error"; 
       return response; 
      } 
      outputStream.writeBytes(lineEnd); 
      outputStream.writeBytes(twoHyphens + boundary + twoHyphens 
        + lineEnd); 

      // Responses from the server (code and message) 
      int serverResponseCode = connection.getResponseCode(); 
      String serverResponseMessage = connection.getResponseMessage(); 
      System.out.println("Server Response Code " + " " + serverResponseCode); 
      System.out.println("Server Response Message "+ serverResponseMessage); 

      if (serverResponseCode == 200) { 
       response = "true"; 
      }else 
      { 
       response = "false"; 
      } 

      fileInputStream.close(); 
      outputStream.flush(); 

      connection.getInputStream(); 
      //for android InputStream is = connection.getInputStream(); 
      java.io.InputStream is = connection.getInputStream(); 

      int ch; 
      StringBuffer b = new StringBuffer(); 
      while((ch = is.read()) != -1){ 
       b.append((char)ch); 
      } 

      String responseString = b.toString(); 
      System.out.println("response string is" + responseString); //Here is the actual output 

      outputStream.close(); 
      outputStream = null; 

     } catch (Exception ex) { 
      // Exception handling 
      response = "error"; 
      System.out.println("Send file Exception" + ex.getMessage() + ""); 
      ex.printStackTrace(); 
     } 
     return response; 
    } 

我才知道,下載文件時,我們可以暫停和恢復下載,並有很多可用的代碼。我嘗試了這些代碼並獲得瞭解決方案。

但我可以暫停並恢復Httpurlconnection中的上傳過程。如果我的問題很愚蠢,請原諒我,因爲我正在學習這個概念。任何人都可以幫我在這裏暫停和恢復上傳,因爲根據我的知識,沒有可用的代碼。

調試後,我得到了這裏的文件上傳大小的響應,在這段代碼,

bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      try { 
       while (bytesRead > 0) { 
        try { 
         outputStream.write(buffer, 0, bufferSize); 
        } catch (OutOfMemoryError e) { 
         e.printStackTrace(); 
         response = "outofmemoryerror"; 
         return response; 
        } 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       response = "error"; 
       return response; 
} 
     } 

希望能得到一些幫助,以更新自己。

+0

流暫停/恢復是兩個客戶端處理'Range'頭域的能力給出的一個特性。如果發送者(使其成爲服務器或客戶端)能夠處理髮送內容塊並且接收者能夠處理重建,則它應該非常類似。 [這是一個擴展它的工作原理](https://superuser.com/questions/641950/how-do-pause-able-downloads-work)。你將不得不調整服務器,以處理部分上傳,我不認爲它可以自己做下載。 – bracco23

+0

@ bracco23是urlconnection.setChunkedStreamingMode(1024),足以將文件從客戶端發送到服務器端? –

+0

它可能會成爲代碼的一部分,但您首先必須確保您的服務器能夠處理以塊形式發送的文件。此外,您還必須處理以保持發送的塊的數量以及要發送的塊的數量才能恢復。 – bracco23

回答

0
``` 
bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
      try { 
       while (bytesRead > 0) { 
        try { 
         outputStream.write(buffer, 0, bufferSize); 
        } catch (OutOfMemoryError e) { 
         e.printStackTrace(); 
         response = "outofmemoryerror"; 
         return response; 
        } 
        bytesAvailable = fileInputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
       response = "error"; 
       return response; 
} 
     } 
    ``` 
相關問題