Download pdf file from Java web app made in JSF

和自甴很熟 提交于 2021-02-11 17:51:36

问题


Hello I have a Java web app made in JSF...when you press a button it should write a string in a pdf file and download it. Here is the code:

String cont = "DATA CREARE: " + data_creare3 + "\n" + "CIF: " + cif3 + "\n" + "ID SOLICITARE: " + id_solicitare3 + "\n" + "DETALII" + detalii3 + "\n" + "TIP: " + tip3 + "\n" + "ID: " + id3;

startDownload(cont.getBytes(), "id.pdf");

This is the String and the method being called and here is the function that makes the download..

private void startDownload(byte[] continut, String denumire) throws IOException{
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // externalContext.responseReset();
    externalContext.setResponseHeader("Cache-Control", "public");
    externalContext.setResponseHeader("Pragma", "public");
    externalContext.setResponseHeader("Content-Type", "application/pdf");
    externalContext.setResponseHeader("Content-Length",
    Integer.toString(continut.length));
    externalContext.setResponseHeader("Content-Disposition","attachment;filename=\"" + denumire + "\"");
    externalContext.getResponseOutputStream().write(continut);
    facesContext.responseComplete();
}

When I click the button to download it says:

emptyResponse: An empty response was received from the server.  Check server error logs

The server error logs say:

2020-02-03 09:18:44,438 WARNING [javax.enterprise.resource.webcontainer.jsf.application] (default task-2) JSF1063: WARNING! Setting non-serializable attribute value into HttpSession (key: logic, value class: com.serban.Logic).

What should I do to make it work ? Thanks in advance

来源:https://stackoverflow.com/questions/60034812/download-pdf-file-from-java-web-app-made-in-jsf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!