2

一旦我将库 icefaces.jar icepush.jar icefaces_ace.jar 添加到我的类路径以使用 ACE 组件,我的 SaveAs 对话框就不会弹出?我不确定这是否是一个错误,但没有类路径中的库它可以工作。这是我的另存为方法:

    public void downloadFile(String propertyPath) throws IOException {

     ProxyFile fileToDownload = repBean.downloadFile(propertyPath);

     FacesContext facesContext = FacesContext.getCurrentInstance();
     ExternalContext externalContext = facesContext.getExternalContext();
     HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

     response.reset();         response.setContentType(fileToDownload.getContentType()); 
     response.setHeader("Content-Length", String.valueOf(fileToDownload.getLength()));
     response.setHeader("Content-disposition", "attachment; filename=\"" + fileToDownload.getName() + "\""); 

     BufferedInputStream input = null;
     BufferedOutputStream output = null;


     try {
         input = new BufferedInputStream(fileToDownload.getContent());
         output = new BufferedOutputStream(response.getOutputStream());

         byte[] buffer = new byte[10240];
         for (int length; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
         }
     } finally {
         output.close();
         input.close();
         facesContext.responseComplete(); 
        }
     }
4

1 回答 1

2

您不能使用 ajax 下载文件。

Ajax 是由 JavaScript 的XMLHttpRequest对象执行的。请求将成功执行,响应将被成功检索。然而,JavaScript 无法将响应写入客户端的磁盘文件系统,也无法强制与给定响应进行“另存为”对话。那将是一个巨大的安全漏洞。

您的具体问题的原因是 ICEfaces 本身。也就是说,当您将 ICEfaces 集成到 JSF Web 应用程序中时,所有标准<h:commandXxx>链接/按钮都会默默地变成启用 ajax 的链接/按钮,这确实会引起初学者的混淆。确保下载链接/按钮没有隐式使用 ICEfaces 引入的 ajax 工具。根据他们关于该主题的 wiki 页面,您需要显式嵌套 a<f:ajax disabled="true">以禁用此功能。

禁用组件的 Ajax

您还可以在单​​个组件级别禁用 Ajax:

<h:commandButton value="Send" actionListener="#{bean.sendMessage}">
    <f:ajax disabled="true"/>
</h:commandButton>

将其应用于您的下载链接/按钮。

于 2012-11-16T18:36:13.857 回答