gpt4 book ai didi

java - 在 ADF 中下载文件仅适用于第一次

转载 作者:行者123 更新时间:2023-12-01 13:16:08 25 4
gpt4 key购买 nike

我有一个ADF表在 JSF绑定(bind)到 View Object 的页面其中保存用户上传的文件。这个View Object包含下载像 blob domain 这样的文件所需的所有信息, file 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>

如您所见,按钮位于 <af:column> 中标签。因此对于每个文件行都有相应的下载按钮。

这是 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();
}
}

最佳答案

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

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

在最后一个语句之前的try block 末尾facesContext.responseComplete();

小改动:

我通过这一行获取outputstream:outputStream = response.getOutputStream();

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

public void downloadSelectedFile(FacesContext facesContext, OutputStream outputStream)

关于java - 在 ADF 中下载文件仅适用于第一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com