Skip to content

Instantly share code, notes, and snippets.

@emanoelqueiroz
Last active February 27, 2017 17:45
Show Gist options
  • Save emanoelqueiroz/8f2cfc333ad43cf55c7da7e263c9db7e to your computer and use it in GitHub Desktop.
Save emanoelqueiroz/8f2cfc333ad43cf55c7da7e263c9db7e to your computer and use it in GitHub Desktop.
Carrega arquivo HTML pelo caminho passado por parâmetro e retorna uma String do conteúdo do mesmo.
/**
* Carrega arquivo HTML pelo caminho passado por parâmetro e retorna uma String do conteúdo do mesmo.
* Esses arquivos devem estar na pasta <b>resources</b> para que o método possa encontra-los.
* <p>
* <code>Ex.: FileUtil.getHtmlContent(Teste.class, "/htmlreport/template.html")</code>
*
* @param actualClass Clase atual
* @param filePath Caminho do arquivo HTML
* @return Conteúdo do arquivo em String
*/
public static String getHtmlContent(Class<?> actualClass, String filePath) {
try {
URL url = actualClass.getClass().getResource(filePath);
if (url == null) {
throw new FileNotFoundException("Caminho do arquivo incorreto: Arquivo não encontrado!");
}
File file = new File(url.getPath());
if (!FilenameUtils.getExtension(file.getName()).equals("html")) {
throw new Exception("O seguinte arquivo não é um arquivo HTML! -> " + file.getName());
}
if (!file.canRead()) {
throw new Exception("Não foi possivel ler o arquivo!");
}
return FileUtils.readFileToString(file, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment