用 Java 将下载的二进制文件写入磁盘
我有一个软件,允许在 javascript 文件(.js)中编写附加组件,允许使用 Java 函数(我不知道这是否常见,我以前从未在 javascript 文件中看到过 java 调用)
我需要下载一个来自网络服务器的二进制文件并将其写入硬盘。我尝试了以下代码:
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();
生成的文件不再是有效的 gzip 存档。如果我犯了一个巨大的错误,我很抱歉,但我不是程序员,对 Java 不太了解。
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)
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要使用
FileWriter
- 尝试将数据转换为文本。
只需使用
FileOutputStream
。Don't use a
FileWriter
- that's trying to convert the data into text.Just use
FileOutputStream
.我知道这个问题已经得到解答,但更简单的方法是使用 Apache Commons IO 的 IOUtils.copy() 方法,该方法可以将
InputStream
完全复制到输出流
。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 anInputStream
to anOutputStream
.DataInputStream
用于读取 Java 原语,而不是通用数据。它也是多余的,因为 urlConnect.getInputStream(); 已经返回一个InputStream,并且所有InputStreams都支持read()。
PS 这假设
is
和bis
是同一个变量。否则,您将在循环中读取错误的流。DataInputStream
is meant for reading Java primitives, not for generic data.It's also redundant, as
urlConnect.getInputStream();
already returns an InputStream, and all InputStreams support read().P.S. This is assuming
is
andbis
are the same variable. Otherwise, you're reading the wrong stream in the loop.刚刚阅读的 LimitInputStream 听起来就像它所做的正是您正在做的事情,缓冲输入流以提高效率。
Just read about LimitInputStream sounds like it does exactly what you are doing, buffering the input stream for greater efficiency.
您甚至可以使用 NIO FileChannel#transferFrom 方法。
参考link1 ,链接2
You can even use NIO FileChannel#transferFrom method.
Reference link1, link2