how to download binary file using jsp at client side

  • Thread starter Girish T Deshpande
  • Start date
G

Girish T Deshpande

I am using Tomcat 4 and wish to download binary file(actualy an excel
and pdf file) to the local disk.
Since out.write ouputs the character array, I get error message when I
pass byte array.

The part of the code is below(I have set proper mime type)

FileReader f = new FileReader("C:/Tengli/jakarta-tomcat-4.1.24/webapps/ROOT/JavaExcel/ouput9.xls");

BufferedInputStream inputStream = new BufferedInputStream(new
FileInputStream("C:/Tengli/jakarta-tomcat-4.1.24/webapps/ROOT/JavaExcel/ouput9.xls"));

ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (true) {
int nBytes = inputStream.read(buffer);
if (nBytes < 0) break;
out.write(buffer, 0, nBytes);
}
//buffer = out.toByteArray();


I also get the error that out is already defined in
_jspService(......).
If I use other variable name instead of out it compiles and runs fine
but the file will be completely empty since it writes at unknown
place.


Could anyone give the entire code!

Thank you
 
S

Sergio Juan

Hi

"out" is a predefined object in the jsp (it is a Writer to
response.getOutputStream()) so use another name.
ITOH, the code looks fine.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top