1

ネットから大きなファイル (1 MB 以上のファイル) をダウンロードできません。ただし、私のプログラムはローカルホストからこれらの大きなファイルをダウンロードできます。大きなファイルをダウンロードするために必要なことは他にありますか? コード スニペットは次のとおりです。

 try {

        //connection to the remote object referred to by the URL.
        url = new URL(urlPath);
        // connection to the Server
        conn = (HttpURLConnection) url.openConnection();

        // get the input stream from conn
        in = new BufferedInputStream(conn.getInputStream());

        // save the contents to a file
        raf = new RandomAccessFile("output","rw");


        byte[] buf = new byte[ BUFFER_SIZE ];
        int read;

        while( ((read = in.read(buf,0,BUFFER_SIZE)) != -1) )
    {

            raf.write(buf,0,BUFFER_SIZE);
    }

    } catch ( IOException e ) {

    }
    finally {

    }

前もって感謝します。

4

1 に答える 1

3

実際に読んだバイト数を無視しています。

while( ((read = in.read(buf,0,BUFFER_SIZE)) != -1) )
{
    raf.write(buf,0,BUFFER_SIZE);
}

呼び出しでいっぱいにしなかった場合でも、write呼び出しは常にreadバッファー全体を書き込みます。あなたが欲しい:

while ((read = in.read(buf, 0, BUFFER_SIZE)) != -1)
{
    raf.write(buf, 0, read);
}
于 2011-10-20T20:37:15.457 に答える