Boa tarde, estou fazendo um botão para download que disponibilizará um arquivo.xml. Até ai tudo bem, porém o arquivo e carregado mas não abre a opção de salvar e nem muito menos o download automático é feito, para facilitar o sistema não me retorna nada, deu debug e não deu erro algum, as variáveis foram carregadas conforme planejado. Podem me ajudar?
public File StringToFileXML() throws IOException {
if (xmlNota != null) {
File arquivo;
arquivo = new File("Arquivo.xml");
FileWriter arquivoWr;
arquivoWr = new FileWriter(arquivo);
arquivoWr.write(xmlNota);
arquivoWr.close();
return arquivo;
} else {
return null;
}
}
public void baixarXML() throws IOException {
File arquivo = StringToFileXML();
String nomeArquivo =arquivo.getName();
// FacesContext facesContext = FacesContext.getCurrentInstance(); //Testar
FacesContext facesContext = super.context();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.responseReset();
externalContext.setResponseContentType("application/xml");
externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"" + nomeArquivo + "\"");
OutputStream out = externalContext.getResponseOutputStream();
byte[] data = Files.readAllBytes(arquivo.toPath());
try (InputStream is = new ByteArrayInputStream(data)) {
int read = -1;
byte[] buffer = new byte[1024];
while ((read = is.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
out.flush();
out.close();
}
facesContext.renderResponse();
facesContext.responseComplete();
}