2011-11-07 1 views
1

で受信したバイトはありません数: 私は実装するコードを以下している:私は、次の作業URL接続からダウンロードされたバイト数を決定したいのjava

....... 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    connection.connect(); 
    InputStream is = connection.getInputStream(); // throws an IOException 
    DataInputStream dis = new DataInputStream(new BufferedInputStream(is));   

    FileOutputStream fos = new FileOutputStream("C:\\Picture.jpeg"); 
    int read =0; 
    byte[] bytes = new byte[1024]; 
    while((read = dis.read(bytes)) != -1) 
    { 
     fos.write(bytes, 0, read); 
    } 
    System.out.println(read + " byte(s) copied"); 

を最後の行からの出力としてあります次のようになります。

Opening connection to http://www.xyz.com//Picture.jpeg... 
    Copying image resource (type: application/jpeg, modified on: 02/02/2010 4:19:21 AM)... 
    -1 byte(s) copied 

私のコードのエラーは何ですか?私は

+3

それを取得しないでください助けてください。ループの後で 'read'は常に' -1'になります。さもなければ、あなたは無限ループに入ります。具体的な問題は何ですか? – home

+0

このリンクに従ってください。これは問題と同じです。 http://stackoverflow.com/questions/6081796/menu-item-enable-disableあなたの問題を解決するのに役立つこのリンクをご覧ください。 http://www.eclipsezone.com/eclipse/forums/t90781.rhtmlおよびhttp://www.odevelop.com/blog/?p=211 –

答えて

3
int read =0; 
    int reddit = 0; 
    byte[] bytes = new byte[1024]; 
    while((read = dis.read(bytes)) != -1) 
    { 
     fos.write(bytes, 0, read); 
     reddit += read; 
    } 
    //your read variable must have the value -1 at this point 
    System.out.println(reddit + " byte(s) copied"); 
+1

重要な注記:新しい変数の名前をアドレスに書き込まないでくださいブラウザのバー! – bpgergo

0
int totalBytes = 0; 
... 
while((read = dis.read(bytes)) != -1) 
{ 
    totalBytes += read; 
    fos.write(bytes, 0, read); 
} 
関連する問題