Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Download files from JSP  RSS feed

 
Amaia Roldan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem downloading files from JSP.
I have use this code to do it:
<%
FileName="prueba9.pdf";
File fichero = new File(FileName);
OutputStream outStream = response.getOutputStream();
BufferedInputStream fileInput= new BufferedInputStream(new FileInputStream(fichero));
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename=prueba9.pdf");

byte [] arrayBytes = null;
int data;
int length = (int)fichero.length();
arrayBytes = new byte[length];
while((data = fileInput.read(arrayBytes, 0, array.length))!=-1) {
outStream.write(array, 0, data);
}
fileInput.close();
outStream.close();
%>
The problem is the file is not well downloaded, and I can't open it. The problem is only from JSP because if I use a servlet it works fine, and I CAN'T USE SERVLET.
Can anybody help me?? Thanks
Amaia aoroldan@teleline.es
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSP are just naturally designed to serve character data, not binary. See the API section on JspWriter. I would have expected it to throw an exception at the point you do a getOutputStream since having out as an automatic variable is part of the JSP API, and out is a JspWriter.
Why can't you use a servlet?
Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!