Java 二进制文件下载提前停止

标签 java swing file download

我正在尝试使用以下代码下载一个 exe 文件,知道为什么它只下载了大约 30% 的文件吗?至少它不会抛出任何异常。

我的主要方法如下所示:new DownloadWorker().execute();

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.SwingWorker;

public final class DownloadWorker extends SwingWorker<Object, Object> {

    @Override
    protected Object doInBackground() throws Exception {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;

        try {
            URL url = new URL("http://download.piriform.com/ccsetup320.exe");
            URLConnection conn = url.openConnection();
            conn.connect();

            int fileLength = conn.getContentLength();

            in = new BufferedInputStream(url.openStream());
            out = new BufferedOutputStream(new FileOutputStream("ccsetup320.exe"));

            byte[] buffer = new byte[4096];
            long total = 0;
            int bytesRead = 0;
            while ( (bytesRead = in.read(buffer)) != -1 ) {
                total += bytesRead;
                System.out.println((int) (total * 100 / fileLength));
                out.write(buffer, 0, bytesRead);
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        } finally {
            if ( out != null ) {
                out.flush();
                out.close();
            }
            if ( in != null ) {
                in.close();
            }
        }

        return null;
    }

    @Override
    protected void done() {

    }

}

谢谢。

最佳答案

您是否尝试过在安装了 Java 7 的情况下使用 Java NIO:

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;


public class TestApp {

    public static void main(String[] args) {
        try {
            URL url = new URL("http://download.piriform.com/ccsetup320.exe");
            ReadableByteChannel rbc = Channels.newChannel(url.openStream());
            FileOutputStream fos = new FileOutputStream("c:/ccsetup320.exe");
            fos.getChannel().transferFrom(rbc, 0, 1 << 24);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

上面的代码经过测试并且工作得很好,我是从类似的问题中得到的here我们可以看到 NIO 如何使编码变得如此简单:)

编辑:

我已更新代码以使用 swingworker,并且它可以毫无问题地下载文件:

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import javax.swing.SwingWorker;

public class JavaApplication174 {

    public static void main(String[] args) {
        SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {

                try {
                    URL google = new URL("http://download.piriform.com/ccsetup320.exe");
                    ReadableByteChannel rbc = Channels.newChannel(google.openStream());
                    FileOutputStream fos = new FileOutputStream("c:/ccsetup320.exe");
                    fos.getChannel().transferFrom(rbc, 0, 1 << 24);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                return this;
            }
        };
        worker.run();
    }
}

编辑2:

您在工作实例上调用 execute() 使用 run() 而对我有用!

关于Java 二进制文件下载提前停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285731/

相关文章:

java - 如何对动态呈现 html 和 css 的 Java 代码进行单元测试?

java - GWT 验证框架 (GWTVF) 与 gwt 2.3?

java - FindBug 说这个并发映射不需要同步

php - 如何检查是否在 php 中设置了 $FILE?

file - 输出到 lisp 中的文本文件

file - 如何使用 COBOL 从文件中删除重复项?

java - 使用 log4j 每天创建新的日志文件

java - JFrame 中的图像不显示

java - 在java应用程序中使用js和css(嵌入式浏览器功能)

java - 大型 JPanel 上的绘制字符串在 Linux 上变得困惑,但在 Windows 上却不然