如何使用该库下载文件并打印出保存的字节?我尝试使用
import static org.apache.commons.io.FileUtils.copyURLToFile;
public static void Download() {
URL dl = null;
File fl = null;
try {
fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
copyURLToFile(dl, fl);
} catch (Exception e) {
System.out.println(e);
}
}
但我无法显示字节或进度条。我应该使用哪种方法?
public class download {
public static void Download() {
URL dl = null;
File fl = null;
String x = null;
try {
fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
OutputStream os = new FileOutputStream(fl);
InputStream is = dl.openStream();
CountingOutputStream count = new CountingOutputStream(os);
dl.openConnection().getHeaderField("Content-Length");
IOUtils.copy(is, os);//begin transfer
os.close();//close streams
is.close();//^
} catch (Exception e) {
System.out.println(e);
}
}
参考方案
如果您正在寻找一种在下载前获取字节总数的方法,则可以从http响应中的Content-Length
标头中获取此值。
如果只需要下载后的最终字节数,则最简单的方法就是检查刚刚写入的文件大小。
但是,如果要显示当前已下载多少字节的进度,则可能需要扩展apache CountingOutputStream
来包装FileOutputStream
,以便每次调用write
方法时,它都会计算通过的字节数并更新进度条。
更新
这是DownloadCountingOutputStream
的简单实现。我不确定您是否熟悉使用ActionListener
,但是它对于实现GUI很有用。
public class DownloadCountingOutputStream extends CountingOutputStream {
private ActionListener listener = null;
public DownloadCountingOutputStream(OutputStream out) {
super(out);
}
public void setListener(ActionListener listener) {
this.listener = listener;
}
@Override
protected void afterWrite(int n) throws IOException {
super.afterWrite(n);
if (listener != null) {
listener.actionPerformed(new ActionEvent(this, 0, null));
}
}
}
这是用法示例:
public class Downloader {
private static class ProgressListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// e.getSource() gives you the object of DownloadCountingOutputStream
// because you set it in the overriden method, afterWrite().
System.out.println("Downloaded bytes : " + ((DownloadCountingOutputStream) e.getSource()).getByteCount());
}
}
public static void main(String[] args) {
URL dl = null;
File fl = null;
String x = null;
OutputStream os = null;
InputStream is = null;
ProgressListener progressListener = new ProgressListener();
try {
fl = new File(System.getProperty("user.home").replace("\\", "/") + "/Desktop/Screenshots.zip");
dl = new URL("http://ds-forums.com/kyle-tests/uploads/Screenshots.zip");
os = new FileOutputStream(fl);
is = dl.openStream();
DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os);
dcount.setListener(progressListener);
// this line give you the total length of source stream as a String.
// you may want to convert to integer and store this value to
// calculate percentage of the progression.
dl.openConnection().getHeaderField("Content-Length");
// begin transfer by writing to dcount, not os.
IOUtils.copy(is, dcount);
} catch (Exception e) {
System.out.println(e);
} finally {
IOUtils.closeQuietly(os);
IOUtils.closeQuietly(is);
}
}
}
使用Java RMI调用Python方法 - java我有一个通过Python web2py创建的远程方法。如何测试和调用Java中的方法?我能够测试该方法是否实现@service.xmlrpc,但是如何测试该方法是否实现@service.run? 参考方案 如果您能做到,我会感到惊讶。 Java RMI需要Java对等体。
Spark Java-合并同一列多行 - java我正在使用Java Spark,并且有1个这样的数据框+---+-----+------+ |id |color|datas | +----------------+ |1 |blue |data1| |1 |red |data2| |1 |orange|data3| |2 |black |data4| |2 | |data5| |2 |yellow| | …
有效地将包含字母的字符串转换为Int-Apache Spark - java我正在使用将用户作为字符串的数据集(即B000GKXY4S)。我想将这些用户中的每一个都转换为int,因此可以在Apache Spark ALS中使用Rating(user:Int,product:Int,rating:Double)类。最有效的方法是什么?最好使用Spark Scala函数或python本机函数。 参考方案 如果只想将任何可匹配的Strin…
Apache CXF客户端代理设置 - java我正在尝试使用的教程来开发Soap Consumer for Soap Service。http://cxf.apache.org/docs/developing-a-consumer.html在“使用上下文设置连接属性”部分中,我正在看下面的代码// Set request context property. java.util.Map<String…
获取远程机器的系统信息(使用Java) - java正如问题的标题所言,我想使用Java获取远程系统的系统信息(例如OS名称,版本等)。但是,在任何人回答这个问题之前,我只想问一下这是否可能,如果可以,那么如何?还有一个问题是,这对于基于Unix和基于Windows的系统都应适用。我尝试搜索Internet,但是(几乎)空白。编辑:Java应用程序将是一个桌面应用程序,它必须具有凭据才能登录到远程系统,但不会…