2

我正在尝试使用以下代码下载一个 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() {

    }

}

谢谢。

4

2 回答 2

2

您是否尝试过使用安装了 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();
        }
    }

}

上面的代码已经过测试并且工作得很好,我从一个类似的问题中得到它,我们可以看到 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()而不是为我工作!

于 2012-07-01T21:42:37.997 回答
0

我认为你的错误在于这个循环,因为 -1 没有被写入(这是标记文件结束的最后一个字节):

        while ( (bytesRead = in.read(buffer)) != -1 ) {
            total += bytesRead;
            System.out.println((int) (total * 100 / fileLength));
            out.write(buffer, 0, bytesRead);
        }

将其更改为:

        do {
            bytesRead = in.read(buffer);
            total += bytesRead;
            System.out.println((int) (total * 100 / fileLength));
            out.write(buffer, 0, bytesRead);
        }while(bytesRead!=-1);
于 2012-07-02T10:52:19.620 回答