2012-03-02 106 views
0

我使用這個代碼從服務器上下載的zip文件從服務器下載zip文件時出錯?

private static InputStream OpenHttpConnection(String urlString) 
     throws IOException 
     { 
      InputStream in = null; 
      int response = -1; 

      URL url = new URL(urlString); 
      URLConnection conn = url.openConnection(); 

      if (!(conn instanceof HttpURLConnection))      
       throw new IOException("Not an HTTP connection"); 

      try{ 
       System.out.println("OpenHttpConnection called"); 

       HttpURLConnection httpConn = (HttpURLConnection) conn; 
       httpConn.setAllowUserInteraction(false); 
       httpConn.setInstanceFollowRedirects(true); 
       httpConn.setRequestMethod("GET"); 
       httpConn.setDoOutput(true); 
       httpConn.setDoInput(true); 
       httpConn.setRequestProperty("content-type", "binary/data"); 

       httpConn.connect(); 

       response = httpConn.getResponseCode(); 

       System.out.println("response is"+response); 
       System.out.println(HttpURLConnection.HTTP_OK); 

       if (response == HttpURLConnection.HTTP_OK) { 
        in = httpConn.getInputStream(); 
        System.out.println("Connection Ok"); 
        return in; 
        }      
      } 
      catch (Exception ex) 
      { 
       throw new IOException("Error connecting");    
      } 
      return in;  
     } 

    private static void saveToInternalSorage(InputStream in,String filename,Context ctx){ 



//fos =openFileOutput(filename, Context.MODE_WORLD_READABLE); 

try { 
    // System.out.println("mypath = "+mypath); 
    //fos = new FileOutputStream(mypath); 
    FileOutputStream fos = (ctx).openFileOutput(filename, Context.MODE_WORLD_READABLE); 

    byte[] buffer=new byte[1024]; 






    int len1 ; 
    while ((len1 = in.read(buffer))!=-1) { 
     fos.write(buffer); 


    } 

    // Use the compress method on the BitMap object to write image to the OutputStream 


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

被下載已損壞的zip文件,該文件的實際大小3.5KB,但下載的文件是5kb。請問代碼有什麼問題請幫忙?

+1

'趕上(異常前) { 擲新IOException異常( 「錯誤連接」); }'......如果在try塊中拋出了其他異常,這段代碼會讓你感到困惑。 – artbristol 2012-03-02 12:51:41

回答

3

while ((len1 = in.read(buffer))!=-1) { 
     fos.write(buffer); 


    } 

您在每次迭代中寫入整個緩衝區(1024個字節)。您應該只寫len1字節(讀取的字節數)。

另一方面,您可能想要考慮使用一些更高級別的抽象庫來處理HTTP和流操作等內容。例如Apache Commons HttpComponentsCommons IO

+0

thanx它的工作...但仍然當我試圖解壓縮它..winzip顯示損壞的文件 – Navdroid 2012-03-02 13:50:10

+0

該文件被髮送base-64編碼也許? – pap 2012-03-02 13:56:46

+0

無論如何檢查和改變爲正常形式? – Navdroid 2012-03-02 13:58:27

0
  httpConn.setDoOutput(false); 
      httpConn.setRequestProperty("Content-Type", "application/octet-stream"); 
      httpConn.setRequestProperty("Content-Length", String.valueOf(file.length()); 

while (... > 0) { 
    fos.write(buffer, 0, len1); 


fos.close(); 
0

只寫入填充到緩衝區中的字節,即只寫入len1個字節。它會解決你的問題,就好像緩衝區沒有被填滿一樣,我們只會寫入那些被讀取的字節。

while ((len1 = in.read(buffer))!=-1) { 
     fos.write(subArray(buffer,len1));  
    } 

//Method to create su-array 
    public byte[] subArray(byte[] arr, int length) { 
     byte temp[] = new byte[length]; 
     for (int i = 0; i < length; i++) { 
      temp[i] = arr[i]; 
     } 
     return temp; 
    }