Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

downlaod file using jsp

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
%>
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the ZipInputStream to read and wrap the OutputStream around ZipOutputStream to stream the file back...Hope it helps
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic