简体   繁体   中英

How to calculate download file Size, Status, Time left and Transfer rate using Selenium Webdriver in java

I write a script to download files from website using Selenium Webdriver in Java. I'm able to download files from website but i want to add more things about download file like file Size, Status, Time left and Transfer rate.

Here my java code :

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();

  }
}

How can calculate download file Size, Status, Time left and Transfer rate.

URLConnection connection = url.openConnection();
long aFileLength = connection.getContentLengthLong();

For the other stuff, google: java file download monitor progress

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM