用Java将下载的二进制文件写入磁盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1477269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a binary downloaded file to disk in Java
提问by radius
I have a software that allow to write add-on in javascript files (.js) that allow to use Java function (I don't know if this is common, I never saw java call in javascript file before)
我有一个允许在允许使用 Java 函数的 javascript 文件 (.js) 中编写附加组件的软件(我不知道这是否常见,我以前从未在 javascript 文件中看到 java 调用)
I need to download a binary file from a webserver and write it to the hard drive. I tried the following code:
我需要从网络服务器下载一个二进制文件并将其写入硬盘驱动器。我尝试了以下代码:
baseencoder = new org.apache.commons.codec.binary.Base64();
url = new java.net.URL("https://server/file.tgz");
urlConnect = url.openConnection();
urlConnect.setDoInput(true);
urlConnect.setDoOutput(true);
urlConnect.setRequestProperty("authorization","Basic "+ java.lang.String(baseencoder.encodeBase64(java.lang.String( username + ":" + password ).getBytes())));
urlConnect.setRequestProperty("content-type","application/x-www-form-urlencoded");
is = new java.io.DataInputStream(urlConnect.getInputStream());
fstream = new FileWriter("C:\tmp\test.tgz");
out = new BufferedWriter(fstream);
while((data = is.read()) != -1){
out.write(data);
}
out.close();
is.close();
The resulting file is no longer a valid gzip archive. I'm sorry if I did a huge error but I'm not a programmer and don't know Java too much.
生成的文件不再是有效的 gzip 存档。如果我犯了一个巨大的错误,我很抱歉,但我不是程序员,也不太了解 Java。
采纳答案by Jon Skeet
Don't use a FileWriter
- that's trying to convert the data into text.
不要使用FileWriter
- 试图将数据转换为文本。
Just use FileOutputStream
.
只需使用FileOutputStream
.
byte[] buffer = new byte[8 * 1024];
InputStream input = urlConnect.getInputStream();
try {
OutputStream output = new FileOutputStream(filename);
try {
int bytesRead;
while ((bytesRead = input.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
回答by Powerlord
DataInputStream
is meant for reading Java primitives, not for generic data.
DataInputStream
用于读取 Java 原语,而不是用于通用数据。
It's also redundant, as urlConnect.getInputStream();
already returns an InputStream, and all InputStreams support read().
它也是多余的,因为urlConnect.getInputStream();
已经返回一个 InputStream,并且所有 InputStreams 都支持 read()。
is = urlConnect.getInputStream();
P.S. This is assuming is
and bis
are the same variable. Otherwise, you're reading the wrong stream in the loop.
PS 这是假设is
并且bis
是相同的变量。否则,您将在循环中读取错误的流。
回答by Tom
I know this question is already answered, but a simpler approach is to use Apache Commons IO's IOUtils.copy()
method, which can fully copy an InputStream
to an OutputStream
.
我知道这个问题已经回答了,但一个简单的方法是使用Apache下议院IO的IOUtils.copy()
方法,它可以完全的复制InputStream
到OutputStream
。
回答by Ben
Just read about LimitInputStream sounds like it does exactly what you are doing, buffering the input stream for greater efficiency.
读一下 LimitInputStream 听起来就像你在做的事情一样,缓冲输入流以提高效率。