2 Replies Latest reply on Mar 10, 2004 11:30 AM by nickman

    uploading a PDF into an entity EJB

    zapa

      hey ppl ,
      i've been strugling with this for a few horus now . I'm trying to upload a pdf file into a entity EJB so that i would be able to download it whenever i'd like .

      One of the big problems i'm having is that i don't know in what format I should save the data into the EJB .

      Theoretically i'd like to serialise then use a blob ... i've only read about blobs and it sounds like it's what i need , however i wasn't able to find a way to cast the bytes from the request Input Stream of the jsp into a Blob object ...

      i'm a bit confused here too since it's my first time dooing this ... any help would be greatly appreciated , thanks

        • 1. Re: uploading a PDF into an entity EJB
          humandoing

          Hi Zapa,

          I am going off the top of my head here, so I might be wrong, but...

          You probably don't want to cast the bytes of the input stream directly into a BLOB. What you probably want is your PDF field on your entity bean to be of type "byte[]".

          A BLOB is essentially just a byte array. So lets say your entity bean has methods:

          public abstract byte[] getPDF();
          public abstract void setPDF(byte[]):

          you can do something like this with your input stream from your JSP:

          ByteArrayOutputStream pdfBytes = new ByteArrayOutputStream();

          int current = -1;
          while ( (current = myJspPdfInputStreamDealy.read()) != -1 ){
          pdfBytes.write( current );
          }

          myEntityBean.setPDF( pdfBytes.toByteArray() );


          That code is not checked, and is just off the top of my head, but it should be pretty close to what you want. You might want to buffer the reader instead of reading one byte at a time, but hopefully that helps.

          • 2. Re: uploading a PDF into an entity EJB

            I got this to work like a champ (with Oracle) using org.jboss.ejb.plugins.cmp.jdbc.ByteArrayBlob.

            On my client, I grabbed the PDF file, read it in as an array of bytes and created the ByteArrayBlob with a ByteArrayOutputStream.

            Sample Code:

             FileInputStream fis = null;
             BufferedInputStream bis = null;
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             try {
             fis = new FileInputStream(fileName);
             bis = new BufferedInputStream(fis);
             byte[] buffer = new byte[2048];
             while(true) {
             int bytesread = bis.read(buffer);
             if(bytesread < 1) break;
             baos.write(buffer, 0, bytesread);
             }
             ByteArrayBlob bab = new ByteArrayBlob(baos.toByteArray());
            


            The ByteArrayBlob is then defined like any other plain java class and it extends java.sql.Blob so the default JDBC handling applied.

            Cheers.

            //Nicholas