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 types (pdf,docx,xlsx) from ADF UI - garbled data

862103Aug 31 2013 — edited Jan 2 2014

Hi

    I am using Jdev Studio Edition Version 11.1.2.3.0 and deployment in integrated web logic server .

Requirement : To be able to store and retrieve attachments of any file types (pdf,docx,doc,xls,xlsx,txt) from ADF UI .

Problem : While downloading, only txt file types , i am able to retrieve and is in readable format. All other files have either garbled data / does not open.

with xlsx : Garbled/ junk data

pdf : Could not open as it is not a supported file type or the file has been damanged .

Docx : does not open.

I had created a BLOB database column in a table to store other information and also the attachement details.

I had created a EO & VO on top of this table and the column to store the file is of type Blobdomain .

I am able to upload all of the above mentioned file types and store in the blob column in DB. I use the below code to achieve that.

public static void copyStream(InputStream input, OutputStream output) throws IOException {

  

byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

  

int count = 0;

  

int n = 0;

  

  1. System.out.println("copystream1");

 

// System.out.println("input.read(buffer)" + input.read(buffer));

  

while (-1 != (n = input.read(buffer))) {

      

  1. System.out.println("input.readbuffer");

      

  • output.write(buffer, 0, n);

  

}

}

  

public BlobDomain getBlob(UploadedFile file) {

  

InputStream in = null;

  

BlobDomain blobDomain = null;

  

OutputStream out = null;

  

try {

      

in = file.getInputStream();

      

      

  1. System.out.println("in" + in.read());

      

blobDomain = new BlobDomain();

     

      

out = blobDomain.getBinaryOutputStream();

      

//System.out.println("out0" + out);

      

copyStream(in, out);

     

// System.out.println("out" + out);

  

} catch (IOException e) {

      

  1. e.printStackTrace();

      

  1. System.out.println("exception1" );

  

} catch (Exception e) {

      

  1. e.fillInStackTrace();

      

  1. System.out.println("exception2" );

  

}

  

finally {

          

try {

              

  1. in.close();

              

  • out.flush();

              

  • out.close();

          

} catch (IOException e) {

              

  1. e.printStackTrace();

          

}

  

}

  

return blobDomain;

}

public void onFileUpload(ValueChangeEvent valueChangeEvent) {

  

file = (UploadedFile)valueChangeEvent.getNewValue();

  

// Get the file name

  

  1. System.out.println("file" + file);

  

fileName = file.getFilename();

  

// get the mime type

  

contentType = file.getContentType();

  

// get blob

  

  1. System.out.println("Bfrblob" + blob);

  

blob = getBlob(file);

  

  1. System.out.println("length" + blob.getLength());

  

DCBindingContainer lBindingContainer =

      

(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();

  

DCIteratorBinding lBinding = lBindingContainer.findIteratorBinding("<iterator name>");

  

Row newRow = lBinding.getCurrentRow();

  

  1. System.out.println("newRow.getAttribute(CNumber) " + newRow.getAttribute("CNumber"));

  

//System.out.println("FileAttach" + blob);

  

//System.out.println("inputFile" + inputFile);

  

  1. newRow.setAttribute("FileAttach", blob);

  

  1. newRow.setAttribute("FileName", fileName);

}

Below code is used to download the file . The content types and the header are changed manually(as of now for testing ) to match the file types .

public void downloadFile(FacesContext facesContext,

                                 OutputStream outputStream) {

            DCBindingContainer lBindingContainer =

(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();

            BlobDomain blob ;

            DCIteratorBinding lBinding = lBindingContainer.findIteratorBinding("<iterator name>");

             blob =

(BlobDomain)lBinding.getCurrentRow().getAttribute("FileAttach");

            String filename =

(String)lBinding.getCurrentRow().getAttribute("FileName");

            HttpServletResponse response = (HttpServletResponse)ectx.getResponse();

            response.setContentType("docx");

response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".docx"+ "\"");

          

            try {

System.out.println("blob.getLength()" + blob.getLength());

              

                OutputStream os = response.getOutputStream() ;

                          

System.out.println("blob.getCharacterOutputStream()" + blob.getCharacterStream());     

System.out.println("filename" + filename);       

                //copyStream(blob.getInputStream(), outputStream);

copyStream(blob.getInputStream(), os);

System.out.println("os" + os.toString());       

             //   blob.closeInputStream();

                //outputStream.flush();

            } catch (IOException e) {

                e.printStackTrace();

            }

            finally {

                    try {

                   

                        outputStream.flush();

                        outputStream.close();

                    } catch (IOException e) {

                        e.printStackTrace();

                    }

            }

        }

Could someone help me in resolving this issue .

Thanks

Ganesh

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 30 2014
Added on Aug 31 2013
14 comments
3,023 views