Save a page from an HTMLPage (java)

2

I have a method that generates an HtmlPage, I would like to save to disk.

public void gerarPaginaIndex() {
    try {
        final HtmlPage paginaIndex = WebClientFactory.getInstance().getPage(URL_INICIAL);
        this.criarPaginaEmDisco(paginaIndex, new File(PATH + "paginaIndex.html"));
    } catch (IOException e) {
        e.printStackTrace();
    }

}

The createPaginaEmDisco () method receives by parameter the address to save and the HtmlPage.

    
asked by anonymous 25.09.2017 / 13:58

2 answers

1

The simplest way to save a file using Java 8 is as follows:

Files.write(Paths.get(PATH + "paginaIndex.html"), paginaIndex.asXml().toString().getBytes(Charset.forName("ISO‌​-8859-1")));
    
25.09.2017 / 14:29
1

The HtmlPage class has the save that you can use in this situation:

public void gerarPaginaIndex() {
try {
    final HtmlPage paginaIndex = WebClientFactory.getInstance().getPage(URL_INICIAL);
    //Salva a página
    paginaIndex.save(new File(PATH + "paginaIndex.html"));
} catch (IOException e) {
    e.printStackTrace();
}

If you want to use the createPaginaEmDisco method, just encapsulate the call to save

public void criaPaginaEmDisco(HtmlPage pagina, File arquivo) throws IOException {
    pagina.save(arquivo);
}
    
25.09.2017 / 14:23