send blob strait to user without file creation

I'm hope to find a way to take a PDF blob from my DB and send it directly to an end user without having to create a temp.pdf file.

Connection conn = ds.getConnection();

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT file FROM tabe");

if (rs.next()){

// retrieve PDF file

Blob pdf = rs.getBlob("file");

//stream to user

}

i got the blob to atleast store in a variable easy enough, but the issue is streaming it to the user.

Blob pdf = rs.getBlob("file");

// setup the streams to process blob

InputStream input = pdf.getBinaryStream();

ByteArrayOutputStream output =new ByteArrayOutputStream();

// set read buffer size

byte[] rb =newbyte[1024];

int ch = 0;

// process blob

while ((ch=input.read(rb)) != -1){

output.write(rb, 0, ch);

}

// transfer to byte buffer

byte[] b = output.toByteArray();

input.close();

output.close();

[1647 byte] By [Luhpsa] at [2007-11-27 6:37:24]
# 1

So your question really has nothing to do with BLOB or even databases, eh?

Isn't it really: I have a byte[] containing a pdf document. How can I display

it to the user without creating a temporary file?

This is where my crystal ball gets cloudy, but as the smoke got thicker,

I think I saw a browser. Is your application a web application?

Hippolytea at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 2
> ByteArrayOutputStream output = new ByteArrayOutputStream();Don't do this. Make "out" be the user. Keep the rest of the code the same.
DrClapa at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 3
If Hyppolite's crystal ball is right, then what you want is to specify the Content-Type so that it mentions you are sending a PDF file. And then the content of the HttpResponse only need to be the PDF's content as retrieved from your blob.
Dalzhima at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 4

No, my question does have to do with a BLOB object.

as it says in the posting title, i want to stream that blob strait to the user.

the second code snip was just something i was working with, but could not create the streaming process i wanted as i do not have the know how.

response.redirect is not an option hear since the file does not exist on the server, and only in a blob object (or any object i send it to) such as the byte array that i have. i was only giving an example of what i had thus far in hopes of comments as to where to go, or alternatives.

yes, it is a web app, i should have specified that, this is being done in a test JSP file.

Luhpsa at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 5

> this is being done in a test JSP file.

Ugh. Now my crystal ball shows me spitting up in my mouth a little. You've stuck database access code into a JSP?

Grab your textbook. In the section on servlets, there is always an example of how to send different content type to the user's browser, like an excel spreadsheet.

Hippolytea at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 6
> this is being done in a test JSP file.First step: stop doing it in a JSP file. The whitespace that you put in your JSP to make it readable is also sent to the user. This will corrupt the PDF and make it unusable. Do this in a servlet.
DrClapa at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 7

I'll show the the code I would use:

public static synchronized int loadBlob( File pix )

throws NullPointerException {

if ( pix == null ) {

System.out.println(FILE_ERR);

return(0);

}

byteb= 0;

byte[] tempix = new byte[50000], // change to suite

pixels = null;

int idx= 0,

size= 0,

count= 0;

String pixName = pix.getName(),

// Note - substitute preparedStatement as desired - just an example.

// Note, in my case the filename is the ID used.

sqlS= "select"

+ "my_table_name.blob_thing"

+ " from my_table_name"

+ " where my_table_name.blob_id = "+pixName

+ "for update",

sqlI= "insert into my_table_name ("

+ "my_table_name.blob_id,"

+ "my_table_name.blob_thing )"

+ " values ("

+ "?,"

+ "EMPTY_BLOB())";

Statementstmt= null;

PreparedStatement pstmt = null;

FileInputStream fis= null;

OutputStreamos= null;

Blobblob= null;

ResultSetrset= null;

try {

/**

* Insert initial row with empty Blob

*/

pstmt = conn.prepareStatement(sqlI);

pstmt.setString(1, pixName);

count = pstmt.executeUpdate();

/**

* Reclaim DB row

*/

stmt = conn.createStatement(); // Connection aquired elsewhere.

rset = stmt.executeQuery(sqlS);

if ( rset.next() ) {

blob= rset.getBlob(1);

os= ((oracle.sql.BLOB)blob).getBinaryOutputStream();

fis= new FileInputStream( pix );

size= fis.read(tempix);

pixels = new byte[size];

for (idx = 0; idx < pixels.length; idx++)

pixels[idx] = tempix[idx];

os.write(pixels);

fis.close();

os.close();

conn.commit();

/

abillconsla at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 8
And finally, I never said to use response.redirectuse the header:Content-Type: application/pdf
Dalzhima at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 9
> I'll show the the code I would use:That's backwards, right? The OP already has the BLOB in the database.And the OP didn't specify Oracle.
Hippolytea at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 10

1 - Hippolyte, stop being a ****, its really un-needed. go to anandtech for that

2 - A servlet and a JSP are almost exactly the same thing. www.google.com has more. a jsp can, in the same exact way, send content type to the users browswer.

my crystal ball shows that you have allot to say without actually saying anything, it also tells me your useless comments are no longer needed, please stop posting in my thread.

Luhpsa at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 11
Okay. Good day to you.
Hippolytea at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 12

> > I'll show the the code I would use:

>

> That's backwards, right? The OP already has the BLOB

> in the database.

> And the OP didn't specify Oracle.

First of all, it seemed to me that the real problem was populating the DB column with the actual BLOB. If I was wrong, I apologize.

Second, I believe you are correct in that the OP did not specify Oracle. However, since Oracle is probably the most popular Enterprise DB around I took a stab at it, thinking that not much would need to be altered if an other DB would be used.

Thirdly, the code posted could be slapped into the DB as a procedure - at least in Oracle. So it would not matter then if we're talking Servlet, App, or whatever.

Thanks for stars, OP.

~Bill

abillconsla at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 13
> 2 - A servlet and a JSP are almost exactly the same> thing. www.google.com has more. a jsp can, in the> same exact way, send content type to the users> browswer.Suit yourself. Clearly you are the expert here.
DrClapa at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 14
> send blob strait to user without file creationBlob Strait is George's brother.~
yawmarka at 2007-7-12 18:05:47 > top of Java-index,Java Essentials,Java Programming...
# 15

> 1 - Hippolyte, stop being a ****, its really

> un-needed. go to anandtech for that

I really wonder how he could have been a **** in this thread. I'd say you're pretty "soupe-au-lait".

> 2 - A servlet and a JSP are almost exactly the same

> thing. www.google.com has more. a jsp can, in the

> same exact way, send content type to the users

> browswer.

>

> my crystal ball shows that you have allot to say

> without actually saying anything, it also tells me

> your useless comments are no longer needed, please

> stop posting in my thread.

Yet even though a jsp can send Content-Type, it may corrupt the content with the extra whitespace it also prints out as it was already pointed out. My Crystal ball shows me that you have no idea of what the similarity is between a jsp and a servlet and that you should stop trying to outsmart people answering your questions.

Dalzhima at 2007-7-21 21:59:38 > top of Java-index,Java Essentials,Java Programming...