java - Download and get the original filename with htmlunit? -
i want create little application download , install/upgrade windows software.
there more , more annoying javascript systems.
i tried phantomjs, can't download.
tried htmlunit , works download or getting original filename.
can not both @ same time. code doesn't work.
package com.example.simpledownloader; import com.gargoylesoftware.htmlunit.browserversion; import com.gargoylesoftware.htmlunit.webclient; import com.gargoylesoftware.htmlunit.html.htmlelement; import com.gargoylesoftware.htmlunit.html.htmlpage; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.util.logging.level; import org.apache.commons.io.filenameutils; public class main { public static void main(string[] args) throws exception { testdownload(); } public static void testdownload() throws ioexception { // turn htmlunit warnings off. java.util.logging.logger.getlogger("com.gargoylesoftware").setlevel(level.off); // init web client , navigate first page. final webclient webclient = new webclient(browserversion.firefox_31); final htmlpage page1 = webclient.getpage("http://www.videohelp.com/software/av-splitter"); // anchor element. string xpath1 = "//*[@id=\"main\"]/div/div/div[11]/table[1]/tbody/tr[3]/td[2]/a[6]"; htmlelement element = (htmlelement) page1.getbyxpath(xpath1).get(0); // extract original filename filepath. string filepath = element.click().geturl().getfile(); string filename = filenameutils.getname(filepath); system.out.println(filename); // download file. inputstream inputstream = element.click().getwebresponse().getcontentasstream(); fileoutputstream outputstream = new fileoutputstream(filename); int read; byte[] bytes = new byte[1024]; while ((read = inputstream.read(bytes)) != -1) { outputstream.write(bytes, 0, read); } // close webclient. webclient.close(); } }
getting filename works downloading doesn't.
i've got error:
exception in thread "main" java.lang.runtimeexception: java.io.filenotfoundexception: c:\users\admin\appdata\local\temp\htmlunit46883917986334906.tmp (the system cannot find file specified)
it's maybe because clicked filename?
actually, clicking twice.
how about:
// extract original filename filepath. page page2 = element.click(); string filepath = page2.geturl().getfile(); string filename = filenameutils.getname(filepath); system.out.println(filename); // download file. inputstream inputstream = page2.getwebresponse().getcontentasstream(); fileoutputstream outputstream = new fileoutputstream(filename); int read; byte[] bytes = new byte[1024]; while ((read = inputstream.read(bytes)) != -1) { outputstream.write(bytes, 0, read); }
Comments
Post a Comment