我编写了一个脚本,使用Java中的Selenium Webdriver从网站下载文件。我可以从网站下载文件,但我想添加更多关于下载文件的内容,如文件大小,状态,剩余时间和传输速率。
这是我的java代码:
public class Downloadfile {
public static void main(String[] args) throws MalformedURLException, IOException {
List<String> uniqurl = new ArrayList();
// Initialize Webdriver driver
WebDriver driver = new HtmlUnitDriver();
// Go to pdf page
driver.get("http://www.banglakitab.com/MaulanaNurulIslamOlipuri.htm");
// get all page urls
List<WebElement> urllist = driver.findElements(By.tagName("a"));
for (WebElement elemnet : urllist) {
String downloadfileurl = elemnet.getAttribute("href").trim();
//check mp3 url
if (downloadfileurl.contains(".mp3")) {
// check unique download file
if (!(uniqurl.contains(downloadfileurl))) {
uniqurl.add(downloadfileurl);
File file = new File(downloadfileurl);
//print file name
System.out.println(file.getName().replaceAll("%20", " "));
//download file
URL url = new URL(downloadfileurl);
InputStream in = new BufferedInputStream(url.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(file.getName().replaceAll("%20", " ")));
for (int i; (i = in.read()) != -1;) {
out.write(i);
}
in.close();
out.close();
}
}
}
// close driver
driver.quit();
}
}
如何计算下载文件的大小,状态,剩余时间和传输速率。
答案 0 :(得分:1)
URLConnection connection = url.openConnection();
long aFileLength = connection.getContentLengthLong();
对于其他东西,谷歌:java文件下载监控进度
希望这有帮助。