JSF文件下载 - 游标保持忙碌,就像等待响应一样

时间:2017-12-12 13:38:31

标签: jsf jsf-2

我有以下JSF下载方法。一切正常:出现下载框,文件下载到我选择的目录后,页面上的光标保持忙碌,好像等待响应一样。因此,我无法点击页面上的任何元素。

public void download()throwsIOException{
    FacesContext con = FacesContext.getCurrentInstance();
    ExternalContext ex = con.getExternalContext();
    File file = new File(DIRECTORY_PATH_HERE + FILENAME_BERE);

    ex.responseReset();
    ex.setResponseContentType("application/octet-stream");

    try {
        ex.setResponseContentLength((int)file.length());
    } catch (IOException e) {
        e.printStackTrace();    
    }

    ex.setResponseHeader("Content-Disposition", "attachment; filename=\"" + FILE_NAME + "\"");


    final HttpServletResponse res = (HttpServletResponse)excon.getResponse();
    ServletOutputStream out = res.getOutputStream();

    FileInputStream fs = new FileInputStream(file); 
    byte[] buf = new byte[ifs.available()];
    int i = 0;

    while((i = fs.read(buf)) != -1){
        out.write(buf);
    }


    out.flush();
    out.close();
    fs.close();
    fc.responseComplete();
}

1 个答案:

答案 0 :(得分:1)

如果你正在使用JSF我强烈建议你包括实用程序库omnifaces它会让你的生活更轻松你的例子会是这样的

public void download() throws IOException {
    Faces.sendFile(file, true);
}