Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to download an excel through JSP

 
Anitha Devi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic