Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

downlaod file using jsp  RSS feed

 
ramandeep mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i am trying to dowload file using jsp but the downloaded file is currupted
code is

<% try {

String ROOT_PATH;
ROOT_PATH = getServletContext().getRealPath("/");
String path = ROOT_PATH +"/WEB-INF/application/"+ "vlsi.zip";
FileInputStream in = new FileInputStream(path);
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment; filename=vlsi.zip");
int i;
while ((i=in.read()) != -1) {
out.write(i);
}
in.close();
out.close();

}
catch(Exception e)
{
out.println("...error while loading: "+e.toString());
}
%>
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you look at the getResourceAsStream() function in ServletContext?
 
ramandeep mann
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi thank you sir

i got the solution i was using

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
and
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
thats why downloaded file was corrupted

i will try the function you told me about
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file was corrupted most probably because you have put the code in a JSP.
JSPs are designed to output text. Most notable they output a carriage return for every carriage return outside of tags in a JSP file. Sending those carriage returns can corrupt the file.

Also you are using a character writer to send a binary file. That won't work. You should be using an OutputStream rather than a Writer.

Suggestions
- write this in a servlet rather than a JSP (its all java code anyway)
- Use response.getOutputStream() to get a stream to write to.
- using getResourceAsStream is a good idea.

It would be something like:
 
amarnath nalumachu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the ZipInputStream to read and wrap the OutputStream around ZipOutputStream to stream the file back...Hope it helps
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!