I am trying to make the user able to download a file
<h:commandLink value="yes" actionListener="#{Bean.readFileFromServer}"/>
the method redFileFromServer
is a method that returns void, it does some logic then it calls anthoer method to return the file to the response
public void exportList() throws IOException {
FacesContext fc = FacesContext.getCurrentInstance();
fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
try {
userLogger.info("Exposting report ");
ec.setResponseContentType("application/octet-stream"); // Check
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + result.getFileName() + "\"");
OutputStream output = ec.getResponseOutputStream();
int octet;
while ((octet = input.read()) != -1) {
output.write(octet);
}
input.close();
output.close();
fc.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
the file downloaded is compressed zip
file, I have 2 problems, first sometimes this works and sometimes not, and I don't know why the behavior is not consistence.Second problem if there is any exception the page is redirected, which is unacceptable duo to my structure design, I need this to work every time and in case of any problem the page won't redirect.