Search code examples
javahtmlunit

Save image from url with HTMLUnit


Is it possible to save an image to hard disk with HTMLUnit by giving it the image URL? If so how?

Regards!


Solution

  • If you're using HtmlUnit then you should have an HtmlPage. There you can get an HtmlImage and save the file this way:

    HtmlImage image = page.<HtmlImage>getFirstByXPath("//img[@src='blah']");
    File imageFile = new File("/path/to/file.jpg");
    image.saveAs(imageFile);
    

    If you do have an URL... then I don't think you need HtmlUnit to download the image.