Kyl*_*yle 13 java apache-commons-io
如何使用库下载文件并打印保存的字节?我试过用
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但我无法显示字节或进度条.我应该使用哪种方法?
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);
}
}
Run Code Online (Sandbox Code Playgroud)
gig*_*dot 13
如果您正在寻找在下载之前获取总字节数的方法,则可以从Content-Length
http响应中的标头获取此值.
如果您只想在下载后获得最终字节数,则最简单的方法是检查您刚写入的文件大小.
但是,如果要显示已下载了多少字节的当前进度,您可能希望扩展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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是用法示例:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Boz*_*zho 11
commons-io有IOUtils.copy(inputStream, outputStream)
.所以:
OutputStream os = new FileOutputStream(fl);
InputStream is = dl.openStream();
IOUtils.copy(is, os);
Run Code Online (Sandbox Code Playgroud)
并且IOUtils.toByteArray(is)
可以用来获取字节.
获得总字节数是另一回事.Streams不会给你任何总数 - 它们只能提供流中当前可用的内容.但由于它是一个流,它可以有更多的来.
这就是为什么http有其特殊的方式来指定总字节数.它位于响应头中Content-Length
.所以你必须打电话url.openConnection()
然后再打电话getHeaderField("Content-Length")
给URLConnection
对象.它将以字符串形式返回字节数.然后使用Integer.parseInt(bytesString)
,你会得到你的总数.
归档时间: |
|
查看次数: |
25729 次 |
最近记录: |