将二进制数据从URL复制到Java中的文件,而无需中间副本 [英] Copy binary data from URL to file in Java without intermediate copy

查看:296
本文介绍了将二进制数据从URL复制到Java中的文件,而无需中间副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新一些旧代码,以从URL而不是从数据库获取一些二进制数据(数据将被移出数据库,并且可以通过HTTP访问)。数据库API似乎直接提供数据作为原始字节数组,而有关代码使用BufferedOutputStream将此数组写入文件中。

I'm updating some old code to grab some binary data from a URL instead of from a database (the data is about to be moved out of the database and will be accessible by HTTP instead). The database API seemed to provide the data as a raw byte array directly, and the code in question wrote this array to a file using a BufferedOutputStream.

我不在所有熟悉Java,但有点google搜索导致我这个代码:

I'm not at all familiar with Java, but a bit of googling led me to this code:

URL u = new URL("my-url-string");
URLConnection uc = u.openConnection();
uc.connect();
InputStream in = uc.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
final int BUF_SIZE = 1 << 8;
byte[] buffer = new byte[BUF_SIZE];
int bytesRead = -1;
while((bytesRead = in.read(buffer)) > -1) {
    out.write(buffer, 0, bytesRead);
}
in.close();
fileBytes = out.toByteArray();

这似乎在大多数时间工作,但我有一个问题, - 我得到一个OutOfMemory错误的数据项与旧代码工作正常。

That seems to work most of the time, but I have a problem when the data being copied is large - I'm getting an OutOfMemoryError for data items that worked fine with the old code.

我猜这是因为这个版本的代码有多个副本的数据

I'm guessing that's because this version of the code has multiple copies of the data in memory at the same time, whereas the original code didn't.

有没有一个简单的方法从一个URL抓取二进制数据,并将其保存在一个文件中而不产生在内存中多个副本的成本?

Is there a simple way to grab binary data from a URL and save it in a file without incurring the cost of multiple copies in memory?

推荐答案

而不是将数据写入字节数组,然后将其转储到文件,您可以通过替换以下内容直接将其写入文件:

Instead of writing the data to a byte array and then dumping it to a file, you can directly write it to a file by replacing the following:

ByteArrayOutputStream out = new ByteArrayOutputStream();

有:

FileOutputStream out = new FileOutputStream("filename");

如果这样做,则不需要调用 out.toByteArray ()。只要确保在完成后关闭 FileOutputStream 对象,例如:

If you do so, there is no need for the call out.toByteArray() at the end. Just make sure you close the FileOutputStream object when done, like this:

out.close();

请参阅 FileOutputStream 了解详情。

这篇关于将二进制数据从URL复制到Java中的文件,而无需中间副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆