2012-03-02 10 views
0

私がダウンロードしたzipファイルが破損している、ファイルの実際のサイズは3.5キロバイトが、ダウンロードしたファイルであるサーバー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ファイルをダウンロードするには、このコードを使用していますコードの問題は何ですか?

+1

'キャッチ(例外の例) { スロー新しいのIOException( "エラー接続します")。 } '... tryブロックに他の例外がスローされた場合、このコードはあなたを混乱させます。 – artbristol

答えて

3

この

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


    } 

あなたは各反復(1024バイト)でバッファ全体を書いています。 len1バイト(読み込みバイト数)だけを書き込む必要があります。

HTTPやストリーム操作のような高レベルの抽象ライブラリを使用することをお勧めします。 Apache Commons HttpComponentsおよびCommons IOなどです。

+0

これはうまくいきました...しかし、まだ解凍しようとすると..winzipが破損したファイルを表示します – Navdroid

+0

ファイルはbase-64でエンコードされていますか? – pap

+0

通常のフォームを確認して変更する必要はありますか? – Navdroid

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; 
    }