-
1. Re: uploading a PDF into an entity EJB
humandoing Mar 10, 2004 11:06 AM (in response to zapa)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
nickman Mar 10, 2004 11:30 AM (in response to zapa)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