【问题标题】:Download file in ADF works for the first time only在 ADF 中下载文件仅适用于第一次
【发布时间】:2014-04-22 08:30:09
【问题描述】:

我在JSF 页面中有一个ADF 表,该表绑定到View Object,其中包含用户上传的文件。这个View Object 包含下载文件所需的所有信息,例如blob domainfile typefile name。每一行都有一个下载按钮,用户可以通过它下载选定的文件。

每件事都是第一次完美运行。问题是当用户按下他/她已经下载的某些文件的下载按钮时,文件会损坏。该文件出现在浏览器的下载部分,但是当我尝试打开它时,它告诉我无法打开该文件,因为不支持文件格式或文件扩展名。

这是JSF页面中的按钮:

<af:column id="c31">
<af:commandButton text="Download" id="cb12" partialSubmit="true">
      <af:fileDownloadActionListener method="#{ITDetalisBean.downloadSelectedFile}"
         filename="#{row.FileName}" contentType="#{row.FileType}"/>
</af:commandButton>
</af:column>

如您所见,按钮位于&lt;af:column&gt; 标记中。所以每个文件行都有对应的下载按钮。

这里是Bean 方法:

    public void downloadSelectedFile(FacesContext facesContext, OutputStream outputStream) 
{
    // get the view object from the application module
    AppModuleImpl appM = (AppModuleImpl)(JSFUtils.getApplicationModule("AppModuleDataControl"));
    ViewObject fileUploadedVO = appM.findViewObject("UplodedFilesViewTransient1");

    // get the file content as blob domain from the current row
    BlobDomain blobDomain=(BlobDomain)fileUploadedVO.getCurrentRow().getAttribute("FileContn");


    // download the file using output stream
    try {
        HttpServletResponse response =
            (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();

        InputStream in = blobDomain.getBinaryStream();
        outputStream = response.getOutputStream();

        byte[] buf = new byte[1024];
        int count;
        while ((count = in.read(buf)) >= 0) {
            outputStream.write(buf, 0, count);
        }

        in.close();
        outputStream.flush();
        outputStream.close();
        facesContext.responseComplete();


    } catch (IOException ex) {
        System.out.println(ex.getMessage());
        ex.printStackTrace();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java jsf oracle11g download oracle-adf


    【解决方案1】:

    通过添加以下内容解决了问题:

    blobDomain.closeInputStream();
    blobDomain.closeOutputStream();
    

    try块的末尾,在最后一条语句之前facesContext.responseComplete();

    小改动:

    我通过这条线得到了outputstreamoutputStream = response.getOutputStream();

    我应该使用方法附带的outputstream 作为参数:

    public void downloadSelectedFile(FacesContext facesContext, OutputStream outputStream)

    【讨论】:

    • 勾选您自己的答案以表明它已解决。
    • @Ravinder 我不能再过两天。
    • 我也认为 facesContext.responseComplete();没有必要,因为输出流已经给你了!
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    相关资源
    最近更新 更多