使用 java apache commons 下载文件?
如何使用该库下载文件并打印保存的字节?我尝试使用
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);
}
}
How can I use the library to download a file and print out bytes saved? I tried using
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);
}
}
but I cannot display bytes or a progress bar. Which method should I use?
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您正在寻找一种在下载前获取总字节数的方法,您可以从 http 响应中的
Content-Length
标头获取此值。如果您只想要下载后的最终字节数,最简单的方法是检查刚刚写入的文件大小。
但是,如果您想显示已下载字节数的当前进度,您可能需要扩展 apache
CountingOutputStream
来包装FileOutputStream
,以便每次write
方法被调用,它计算通过的字节数并更新进度条。更新
这是
DownloadCountingOutputStream
的简单实现。我不确定您是否熟悉使用ActionListener
但它是实现 GUI 的有用类。这是使用示例:
If you are looking for a way to get the total number of bytes before downloading, you can obtain this value from the
Content-Length
header in http response.If you just want the final number of bytes after the download, it is easiest to check the file size you just write to.
However if you want to display the current progress of how many bytes have been downloaded, you might want to extend apache
CountingOutputStream
to wrap theFileOutputStream
so that everytime thewrite
methods are called it counts the number of bytes passing through and update the progress bar.Update
Here is a simple implementation of
DownloadCountingOutputStream
. I am not sure if you are familiar with usingActionListener
or not but it is a useful class for implementing GUI.This is the usage sample :
commons-io 有
IOUtils.copy(inputStream,outputStream)
。所以:并且可以使用 IOUtils.toByteArray(is) 来获取字节。
获取总字节数是另一回事。流不会为您提供任何总数 - 它们只能为您提供流中当前可用的内容。但既然它是一个流,它可以有更多的东西。
这就是为什么 http 有其特殊的方式来指定总字节数。它位于响应标头
Content-Length
中。因此,您必须调用url.openConnection()
,然后对URLConnection
对象调用getHeaderField("Content-Length")
。它将以字符串形式返回字节数。然后使用 Integer.parseInt(bytesString) 即可得到总数。commons-io has
IOUtils.copy(inputStream, outputStream)
. So:And
IOUtils.toByteArray(is)
can be used to get the bytes.Getting the total number of bytes is a different story. Streams don't give you any total - they can only give you what is currently available in the stream. But since it's a stream, it can have more coming.
That's why http has its special way of specifying the total number of bytes. It is in the response header
Content-Length
. So you'd have to callurl.openConnection()
and then callgetHeaderField("Content-Length")
on theURLConnection
object. It will return the number of bytes as string. Then useInteger.parseInt(bytesString)
and you'll get your total.