wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android 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
Marshal

Joined: Mar 22, 2005
Posts: 41148
    
  45
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.)


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: Trying to download an excel through JSP
 
Similar Threads
zip files without path info...
problem when downloading excel sheet from struts application using Internet explorer
problem with download manager in IE browser.
Reading and Writing Large Files
Download file HELP?