0

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?

t j
  • 7,026
  • 12
  • 46
  • 66
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • Possible duplicate of [How to provide a file download from a JSF backing bean?](http://stackoverflow.com/questions/9391838/how-to-provide-a-file-download-from-a-jsf-backing-bean) – sinclair Mar 23 '16 at 22:23
  • Why aren't you using `Files.copy(file.toPath(), output);` as shown in the above duplicate which was already presented to you before? – BalusC Mar 24 '16 at 08:25
  • In which line do you mean? – Peter Penzov Mar 24 '16 at 08:32

0 Answers0