Question

I have an ADF Table in JSF page that is bounded to View Object which holds user's uploaded files. This View Object contains all the information needed to download a file like blob domain , file type and file name. For each row there is a download button that enables the user to download the selected file.

Every things works perfectly for the first time. Problem is when the user press the download button for some file he/she already download it, the file get corrupted. The file appears in the download section for browser, but when I tried to open it tells that the file can not be open because the file format or file extension is not supported.

here is the button in the JSF page:

<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>

As you can see the button is placed in <af:column> tag. So for each file row there is corresponding download button.

Here is the Bean method:

    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();
    }
}
Was it helpful?

Solution

Problem solved by adding the following:

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

at the end of try block before the last statement facesContext.responseComplete();

Minor Change:

I was getting the outputstream by this line: outputStream = response.getOutputStream();

Instead I should use the outputstream that's come with method as an argument:

public void downloadSelectedFile(FacesContext facesContext, OutputStream outputStream)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top