aspose file tools
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


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Trying to download an excel through JSP" Watch "Trying to download an excel through JSP" New topic
Author

Trying to download an excel through JSP

Anitha Devi
Greenhorn

Joined: Mar 25, 2008
Posts: 1
Hi,

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

response.setContentType("application/vnd.ms-excel");
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 = inst.read(b)) != -1) {
outst.write(b, 0, len);
}
inst.close();
outst.flush();
outst.close();
%>

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.

Regards
Anitha
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42952
    
  73
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: http://aspose.com/file-tools
 
subject: Trying to download an excel through JSP