无法从JSF代码下载文件

时间:2016-03-23 21:25:19

标签: jsf jsf-2.2

我尝试使用此文件在JSF应用程序中下载文件:

<h:form>
    <h:commandLink value="Download brochure." action="#{support.downloadFile()}" />
</h:form>

托管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();
    }
}

但是当我选择Save Us文件时没有任何反应。你知道为什么没有开始数据传输吗?

0 个答案:

没有答案