Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Download file in ADF works for the frist time only

Salman5Mar 17 2014 — edited Mar 17 2014

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.

I am using Jdeveloper 11.1.2.3 with ADF Technology

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();
  
}
}
This post has been answered by Cvele_new_account on Mar 17 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 14 2014
Added on Mar 17 2014
3 comments
444 views