I tried to use this file to download file in JSF application:
<h:form>
<h:commandLink value="Download brochure." action="#{support.downloadFile()}" />
</h:form>
Managed bean
@Named
@ViewScoped
public class Support implements Serializable
{
private static final long serialVersionUID = 1L;
public void downloadFile() throws IOException
{
File file = new File("/opt/public/support/support.pdf");
byte[] buf;
try (InputStream fis = new FileInputStream(file))
{
buf = new byte[1024];
int offset = 0;
int numRead = 0;
while ((offset < buf.length) && ((numRead = fis.read(buf, offset, buf.length - offset)) >= 0))
{
offset += numRead;
}
}
HttpServletResponse response
= (HttpServletResponse) FacesContext.getCurrentInstance()
.getExternalContext().getResponse();
response.setContentType("application/octet-stream");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Type", ????????);
response.setHeader("Content-Disposition", "attachment;filename=support.pdf");
response.getOutputStream().write(buf);
response.getOutputStream().flush();
response.getOutputStream().close();
FacesContext.getCurrentInstance().responseComplete();
}
}
But when i select Save Us file nothing happens. Do you have any idea why data transfer is not starting?