stream file from oracle db

Trying to stream a file directly from the DB to user for DL by the user without having to actually write the file out to the server.

I got this far, problems currently are that XLS/PDF file types come up damaged, the TXT files are incompleate. its as if it times out before being able to fully stream, but i can't seem to figure out why.

Possible buffer problem?

Header info wrong, forcing timeout?

help is appriciated.

try{

testconn = datasource.getConnection();

statement = testconn.createStatement();

resultset = statement.executeQuery("SELECT file_file FROM testtable");

Blob pdf;

byte[] bytes =null;

if (resultset.next()){

pdf = resultset.getBlob("file_file");

InputStream in = pdf.getBinaryStream();

ByteArrayOutputStream out =new ByteArrayOutputStream();

byte[] buffer =newbyte[1024];

int len = 0;

while ((len=in.read(buffer)) != -1){

out.write(buffer, 0, len);

}

byte[] outBytes = out.toByteArray();

response.setHeader("Expires","0");

response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");

response.setHeader("Pragma","public");

response.setHeader("Content-Disposition","attachment; filename=\"test.pdf\"");

// setting the content type

// response.setContentType("application/vnd.pdf");

// response.setContentType("application/vnd.excel");

response.setContentType("html/text");

// the contentlength is needed for MSIE!!!

// response.setContentLength(out.size());

response.getOutputStream().println(out.toString());//"<html><body>" + out.toString() + "</body></html>");

// write ByteArrayOutputStream to the ServletOutputStream

// ServletOutputStream sos = response.getOutputStream();

// out.writeTo(sos);

out.flush();

out.close();

in.close();

}

resultset.close();

statement.close();

testconn.close();

}// end try

[3179 byte] By [Luhpsa] at [2007-11-27 8:01:55]
# 1

> response.getOutputStream().println(out.toString());

Ouch. Don't make a String of it, just pass through all bytes from the InputStream.

> response.setContentType("html/text");

This one should be set to the corresponding data type. "html/text" is wrong, by the way, it'd be "text/html".

quittea at 2007-7-12 19:44:02 > top of Java-index,Java Essentials,Java Programming...