2011-09-19 372 views
2

我使用HttpURLConnection的這需要大約3秒,以用於所有的頭一個5MB的文件上傳的圖像文件,但我打開與.getInputStream()的InputStream的那一刻,該方法需要大約8+秒返回流用。這是一個問題,因爲如果我有多個上傳圖片,上傳進度條會給用戶帶來不好的用戶體驗,他們在每次上傳之間會有相當大的停頓時間,因此進度條僅停留在上傳之間幾秒鐘。我做了一些Google搜索,但沒有人似乎有問題呢?HTTPURLConnection.getInputStream()需要很長時間?

通常我會假定服務器是緩慢的,但看到上載只需要幾秒鐘,下載單詞「成功」或「失敗」真的不應該是太大的問題的!

繼承人一些代碼!我最初是否設置了錯誤? 注:這是又一個的AsyncTask內

ByteArrayInputStream fileInputStream = null; 

    try { 
     fileInputStream = new ByteArrayInputStream(dObject.Data); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 


    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    String Tag="3rd"; 
    try 
    { 
     //------------------ CLIENT RE QUEST   
     Log.e(Tag,"Inside second Method");  

     // Open a HTTP connection to the URL  
     URL url = new URL(_urlString); 
     //connectURL is a URL object 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     // Allow Inputs 
     conn.setDoInput(true); 

     // Allow Outputs 
     conn.setDoOutput(true); 

     // Don't use a cached copy. 
     conn.setUseCaches(false); 

     // Use a post method. 
     conn.setRequestMethod("POST"); 

     conn.setRequestProperty("Connection", "Keep-Alive"); 

     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

     DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); 

     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     //dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + _fileLocation +"\"" + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + _fileLocation +"\"" + lineEnd); 

     dos.writeBytes(lineEnd); 

     Log.e(Tag,"Headers are written"); 

     // create a buffer of maximum size 
     int bytesAvailable = fileInputStream.available(); 
     int maxBufferSize = 1024; 
     int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     byte[] buffer = new byte[bufferSize]; 



     // read file and write it into form... 
     int bytesRead = fileInputStream.read(buffer, 0, bufferSize); 


     while (bytesRead > 0) { 
      dos.write(buffer, 0, bufferSize); 

      //int value = (int)(((float)((float)totalRead/(float) fileSize)) * 100); 

      totalRead += bytesRead; 
      //Publish the progress out to be displayed 
      publishProgress(totalRead); 



      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     } 

     // send multipart form data necesssary after file data... 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 


     // close streams 
     Log.e(Tag,"File is written"); 
     fileInputStream.close(); 
     dos.flush(); 

     Log.e("TIME", "PRE GETINPUTSTREAM"); 
     InputStream is = conn.getInputStream(); 
     Log.e("TIME", "POST GETINPUTSTREAM"); 
     // retrieve the response from server 
     int ch; 


     //Build the respose and log 
     StringBuilder b =new StringBuilder(); 
     while((ch = is.read()) != -1){ 
      b.append((char)ch); 
     } 
     String s=b.toString();  
     Log.i("Response",s); 
     dos.close(); 

     return; 
    } 
    catch (MalformedURLException ex) 
    { 
     ErrorHandler.get().e("3"); 
    } 

    catch (IOException ioe) 
    { 
     ErrorHandler.get().e("2"); 
    } 

回答

4

通常我會假定服務器是緩慢的,但看到只上傳需要幾秒鐘,下載單詞「成功」或「失敗」應該不是真正的問題!

我懷疑它確實是服務器速度慢或超載。

  • 服務器可能會排隊HTTP請求,並且只能並行處理一個小數。

  • 或者,它可能在包含該文件的響應寫入響應之前進行一些數據庫活動的瓶頸。

  • 或者它可以是產生上飛的文件到從緩衝器到HTTP響應的內存中緩衝液(慢),然後流(快速)。

  • 或者其他的解釋是這樣的...

(理論上也有可能是有一些有趣的事情,減慢了請求到服務器的傳遞。我認爲這是儘管不太可能)


您是否嘗試過使用網絡瀏覽器下載相同的文件?你在那裏得到同樣的行爲嗎?

+1

事實上它的服務器正在緩慢其實..我已經受夠了說「上傳」,而上傳,然後顯示具有不確定進度欄的‘敲定’在等待掰過來服務器響應!我認爲我的公司應該升級主機! – Micky

0

在我來說,我發現的getInputStream看起來慢,因爲這種方法初始化SSL握手(在HTTPS URL調用)。第一個電話後,其他電話是OK