无法使用Java下载数据文件

发布于 2024-12-28 03:00:52 字数 1298 浏览 1 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2025-01-04 03:00:52

看起来您正在尝试使用 HTTP 协议下载二进制文件。这实际上可以通过更简单的方式完成:

final URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/9/94/Giewont_and_Wielka_Turnia.jpg");  //1
final InputStream input = url.openStream();  //2
final OutputStream output = new BufferedOutputStream(new FileOutputStream("giewont.jpg")); //3
IOUtils.copy(input, output);  //4
input.close();  //5
output.close();

步骤:

  1. 创建指向您要下载的资源的URL
  2. openStream() 逐字节读取文件。底层 URL 实现处理套接字、Content-length 等。
  3. 创建一个空文件来保存数据。
  4. 输入的内容复制到输出。我正在使用 IOUtils 来自 Apache commons-io 以避免无聊和错误 -易于循环复制。
  5. 关闭两个流。您需要关闭输入来关闭实际的套接字(也许一旦流结束它就会隐式发生?)和输出来刷新缓冲区。
  6. ...就是这样!

请注意,由于您基本上是逐字节复制数据,因此文本文件(无论编码如何)和二进制文件都会正确传输。

Looks like you're trying to download a binary file using HTTP protocol. This can actually be done in much easier way:

final URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/9/94/Giewont_and_Wielka_Turnia.jpg");  //1
final InputStream input = url.openStream();  //2
final OutputStream output = new BufferedOutputStream(new FileOutputStream("giewont.jpg")); //3
IOUtils.copy(input, output);  //4
input.close();  //5
output.close();

In steps:

  1. Create URL pointing to a resource you want to download.
  2. openStream() to read the file byte-by-byte. The underlying URL implementations handles sockets, Content-length, etc.
  3. Create an empty file to save data to.
  4. Copy the contents of input to output. I am using IOUtils from Apache commons-io to avoid boring and error-prone copying in a loop.
  5. Close both streams. You need to close the input to close the actual socket (maybe it happens implicitly once the streams ends?) and output to flush the buffers.
  6. ...that's it!

Note that since you are basically copying the data byte-by-byte, both text files (irrespective to encoding) and binary files are transferred correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文