I am trying to download a pdf file from a Java page.
This is the following code :
String format = "pdf";
String reportName = "test" + "." + format;
byte[] content = newsletterManager.getPdfFile(selectedNewsletterInstance.getIdentity());
final FacesContext fc = FacesContext.getCurrentInstance();
final HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse();
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + reportName);
response.setContentType("application/" + format);
OutputStream ouputStream = null;
try {
ouputStream = response.getOutputStream();
ouputStream.write(content);
} catch (IOException e) {
e.printStackTrace();
}
PrintStream ps = null;
try {
ps = new PrintStream(ouputStream, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ps.flush();
fc.responseComplete();
I'm sure that content
is good because I used it to create a local file on my PC and it worked well. It created a pdf doc that i could open and read.
I can't find why, but it just does nothing when executing the code above.
EDIT:
Here is the header of the server's response :
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-disposition: attachment; filename=test.pdf
Content-Type: application/pdf
Transfer-Encoding: chunked
Date: Wed, 03 Nov 2021 14:48:41 GMT
Header of request :
POST /newsletterinterface.xhtml HTTP/1.1
Host: localhost:8443
Connection: keep-alive
Content-Length: 968
sec-ch-ua: "Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/xml, text/xml, */*; q=0.01
Faces-Request: partial/ajax
X-Requested-With: XMLHttpRequest
sec-ch-ua-platform: "Windows"
Origin: https://localhost:8443
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://localhost:8443/oxway/newsletterinterface.xhtml
Accept-Encoding: gzip, deflate, br
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7