缓冲区长度大于1时下载的文件已损坏

时间:2018-12-28 13:21:06

标签: java inputstream outputstream

我正在尝试编写一个功能,该功能可以通过特定的URL下载文件。该函数会生成损坏的文件,除非我将缓冲区设置为大小为1的数组(如下面的代码所示)。

缓冲区初始化(我打算使用)上方的三元语句以及非1的硬编码整数值将产生损坏的文件。

注意:MAX_BUFFER_SIZE是一个常数,在我的代码中定义为8192(2 ^ 13)。

public static void downloadFile(String webPath, String localDir, String fileName) {
    try {
        File localFile;
        FileOutputStream writableLocalFile;
        InputStream stream;

        url = new URL(webPath);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        int size = connection.getContentLength(); //File size in bytes
        int read = 0; //Bytes read

        localFile = new File(localDir);

        //Ensure that directory exists, otherwise create it.
        if (!localFile.exists())
            localFile.mkdirs();

        //Ensure that file exists, otherwise create it.
        //Note that if we define the file path as we do below initially and call mkdirs() it will create a folder with the file name (I.e. test.exe). There may be a better alternative, revisit later.
        localFile = new File(localDir + fileName);
        if (!localFile.exists())
            localFile.createNewFile();

        writableLocalFile = new FileOutputStream(localFile);
        stream = connection.getInputStream();

        byte[] buffer;
        int remaining;
        while (read != size) {
            remaining = size - read; //Bytes still to be read
            //remaining > MAX_BUFFER_SIZE ? MAX_BUFFER_SIZE : remaining
            buffer = new byte[1]; //Adjust buffer size according to remaining data (to be read).

            read += stream.read(buffer); //Read buffer-size amount of bytes from the stream.
            writableLocalFile.write(buffer, 0, buffer.length); //Args: Bytes to read, offset, number of bytes
        }

        System.out.println("Read " + read + " bytes.");

        writableLocalFile.close();
        stream.close();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

之所以这样写,是因为我可以在用户下载时向其提供实时进度条。我已将其从代码中删除,以减少混乱。

2 个答案:

答案 0 :(得分:1)

len = stream.read(buffer);
read += len;
writableLocalFile.write(buffer, 0, len); 

不得将buffer.length用作读取的字节,而需要使用read调用的返回值。因为它可能返回短读,然后缓冲区在读取的字节之后包含垃圾(0字节或先前读取的数据)。

除了计算剩余空间并使用动态缓冲区外,它只需要16k或类似的值。最后一读很短,没关系。

答案 1 :(得分:1)

InputStream.read()可能读取的字节数少于您的请求。但是,您总是将整个缓冲区附加到文件中。您需要捕获实际的读取字节数,然后仅将这些字节追加到文件中。

另外:

  1. 注意InputStream.read()返回-1(EOF)
  2. 服务器可能返回错误的大小。因此,支票read != size很危险。我建议不要完全依赖Content-Length HTTP字段。取而代之的是,只要继续从输入流中读取内容,直到遇到EOF。