如何在JSF中下载*.txt或*.log文件

如何在JSF中下载*.txt或*.log文件,jsf,download,text-files,Jsf,Download,Text Files,我想下载一个保存在JSF硬盘中的.txt/.log文件,我没有收到任何错误,但问题是我无法下载该文件,需要一些帮助 注意:我正在尝试先压缩文件,然后再下载。 我试过: response.setContentType("text/html"); response.setContentType("text/plain"); page.xhtml中的代码: <h:form> <a4j:outputPanel id="downloadPanel">

我想下载一个保存在JSF硬盘中的.txt/.log文件,我没有收到任何错误,但问题是我无法下载该文件,需要一些帮助

注意:我正在尝试先压缩文件,然后再下载。

我试过:

response.setContentType("text/html");
response.setContentType("text/plain");
page.xhtml中的代码:

 <h:form>     
<a4j:outputPanel id="downloadPanel">
                   <table><tr>
                    <td>
                        <h:commandButton id="dldFiles" title="Download File" image="/images/download.png"  
                                            style="width:20px; height:20px;"/>
                    </td>
                    <td>
                        <h:outputText value="Download log file" style="font-size: 11px; color:#56ADF8; font-weight: bold; cursor:pointer;"/>
                   </td>
                    </tr></table>
                    <a4j:support event="onclick" action="#{sqlLoaderAction.downloadFile}" reRender="uploadForm"></a4j:support>
                </a4j:outputPanel>

            </rich:panel> 
</h:form>
执行上述方法后,我进入屏幕下方:


谢谢….

您不能通过ajax下载文件。由于安全原因,JavaScript没有强制另存为对话的工具。在您的特定情况下,它所能做的最好的事情就是内联显示响应


去掉
,通过将
操作
方法直接放入

中,使其成为一个完全值得的同步请求,我认为代码不会运行良好,因为代码在JSF托管bean中,并且在服务器端运行,所以文件将在运行应用服务器的系统上下载,现在你需要做的是检查,使用两台电脑,
在一台电脑中部署web并尝试从另一台电脑下载文件,然后检查代码的行为,如果文件可以在客户端电脑中下载,那么这很好。从其他方面来说,您需要找到替代方案

这是满足将文本文件下载到客户端系统需要的代码

 public String downloadFileText() {
    File file = new File(GlobalPath);
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  

    response.setHeader("Content-Disposition", "attachment;filename=file.txt");  
    response.setContentLength((int) file.length());  
    ServletOutputStream out = null;  
    try {  
        FileInputStream input = new FileInputStream(file);  
        byte[] buffer = new byte[1024];  
        out = response.getOutputStream();  
        int i = 0;  
        while ((i = input.read(buffer)) != -1) {  
            out.write(buffer);  
            out.flush();  
        }  
        FacesContext.getCurrentInstance().getResponseComplete();  
    } catch (IOException err) {  
        err.printStackTrace();  
    } finally {  
        try {  
            if (out != null) {  
                out.close();  
            }  
        } catch (IOException err) {  
            err.printStackTrace();  
        }  
    }  
    return null;
}

看看这个答案谢谢你,巴卢斯。。。在过去的24小时里我一直在胡思乱想。。我将page.xhtml中的ajax代码替换为以下代码:代码确实非常混乱,但如果仔细阅读(或复制粘贴并重命名/重构/清理),您会发现它在写入本地磁盘文件系统后实际上会写入响应体。
 public String downloadFileText() {
    File file = new File(GlobalPath);
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  

    response.setHeader("Content-Disposition", "attachment;filename=file.txt");  
    response.setContentLength((int) file.length());  
    ServletOutputStream out = null;  
    try {  
        FileInputStream input = new FileInputStream(file);  
        byte[] buffer = new byte[1024];  
        out = response.getOutputStream();  
        int i = 0;  
        while ((i = input.read(buffer)) != -1) {  
            out.write(buffer);  
            out.flush();  
        }  
        FacesContext.getCurrentInstance().getResponseComplete();  
    } catch (IOException err) {  
        err.printStackTrace();  
    } finally {  
        try {  
            if (out != null) {  
                out.close();  
            }  
        } catch (IOException err) {  
            err.printStackTrace();  
        }  
    }  
    return null;
}