gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:35:37 24 4
gpt4 key购买 nike

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

你在你的 worker 实例上调用 execute() 使用 run() 而不是为我工作!

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com