File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Trying to download an excel through JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Trying to download an excel through JSP" Watch "Trying to download an excel through JSP" New topic

Trying to download an excel through JSP

Anitha Devi

Joined: Mar 25, 2008
Posts: 1

Iam trying to download an excel using JSP. Below is the code iam using currently.

response.setHeader("Content-disposition", "attachment; filename=TestXL.xls");

File file = new File("D:\\doc_location\\TestXL.xls");

InputStream inst = new FileInputStream(new File(file.getAbsolutePath()));
ServletOutputStream outst = response.getOutputStream();
byte[] b = new byte[4 * 1024];
int len = 0;

while ((len = != -1) {
outst.write(b, 0, len);

When I run this program, an excel gets opens with junk characters. I face the same problem when I try to open a word document by changing the respective contenttype. But it works fine for pdf & txt file. Kindly help.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
Welcome to JavaRanch.

JSPs are not meant for streaming binary data; you should use a servlet instead. (It may work for some file formats -as you have found out- but not generally.)
I agree. Here's the link:
subject: Trying to download an excel through JSP
It's not a secret anymore!