无法使用Java下载数据文件
我需要使用 Java 下载文件。我可以使用此代码下载文本文件。但我在下载图像[数据]文件时遇到问题。它们被写入损坏的磁盘。我在这里做错了什么?
FileOutputStream fileOutputStream = new FileOutputStream(url
.getPath().substring(url.getPath().lastIndexOf("/") + 1));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = "";
long l = 0;
while (!(line = bufferedReader.readLine()).isEmpty()) {
System.out.println(line);
if (line.contains("Content-Length:")) {
l = Long.parseLong(line.substring(
"Content-Length:".length()).trim());
}
}
byte[] bytes = new byte[socket.getSendBufferSize()];
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(fileOutputStream));
int x = 0;
long fullLength = 0;
int length = 0;
DataInputStream dataInputStream = new DataInputStream(
socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(
fileOutputStream);
while (fullLength < l
&& (length = dataInputStream.read(bytes)) != -1) {
dataOutputStream.write(bytes, 0, length);
System.out.print(length + " ");
bufferedWriter.flush();
fullLength += length;
}
fileOutputStream.flush();
bufferedWriter.close();
socket.close();
I need to download a file using Java. I can use this code to download text files. But I have a problem downloading image[data] files. They are written in to the disk corrupted. What have I done here wrong?
FileOutputStream fileOutputStream = new FileOutputStream(url
.getPath().substring(url.getPath().lastIndexOf("/") + 1));
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = "";
long l = 0;
while (!(line = bufferedReader.readLine()).isEmpty()) {
System.out.println(line);
if (line.contains("Content-Length:")) {
l = Long.parseLong(line.substring(
"Content-Length:".length()).trim());
}
}
byte[] bytes = new byte[socket.getSendBufferSize()];
BufferedWriter bufferedWriter = new BufferedWriter(
new OutputStreamWriter(fileOutputStream));
int x = 0;
long fullLength = 0;
int length = 0;
DataInputStream dataInputStream = new DataInputStream(
socket.getInputStream());
DataOutputStream dataOutputStream = new DataOutputStream(
fileOutputStream);
while (fullLength < l
&& (length = dataInputStream.read(bytes)) != -1) {
dataOutputStream.write(bytes, 0, length);
System.out.print(length + " ");
bufferedWriter.flush();
fullLength += length;
}
fileOutputStream.flush();
bufferedWriter.close();
socket.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试使用 HTTP 协议下载二进制文件。这实际上可以通过更简单的方式完成:
步骤:
URL
。openStream()
逐字节读取文件。底层URL
实现处理套接字、Content-length
等。输入
的内容复制到输出
。我正在使用IOUtils
来自 Apachecommons-io
以避免无聊和错误 -易于循环复制。输入
来关闭实际的套接字(也许一旦流结束它就会隐式发生?)和输出
来刷新缓冲区。请注意,由于您基本上是逐字节复制数据,因此文本文件(无论编码如何)和二进制文件都会正确传输。
Looks like you're trying to download a binary file using HTTP protocol. This can actually be done in much easier way:
In steps:
URL
pointing to a resource you want to download.openStream()
to read the file byte-by-byte. The underlyingURL
implementations handles sockets,Content-length
, etc.input
tooutput
. I am usingIOUtils
from Apachecommons-io
to avoid boring and error-prone copying in a loop.input
to close the actual socket (maybe it happens implicitly once the streams ends?) andoutput
to flush the buffers.Note that since you are basically copying the data byte-by-byte, both text files (irrespective to encoding) and binary files are transferred correctly.