我正在尝试使用以下代码下载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() {
}
}
感谢。
答案 0 :(得分:2)
您是否尝试过使用Java NIO,因为您已安装Java 7:
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()
代替我工作!
答案 1 :(得分: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);