Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with file download dialogue box

 
kumar kiran
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my java application i am using tomcat5.0 server and struts framework.I am facing one problem with file download dialogue box.In a jsp i am having a link, after clicking that link through a javascript function submitting the form the jsp and then control is going to struts action class.In that action class i using the following lines code to get file download dialogue box.

response.setContentType("application/octet-stream");
response.setHeader ("Content-Disposition","attachment;filename=\""+filename+"\"");
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
once i save or open or cancel that dialogue box and then click any submit button in that jsp again the same dialogue box is coming .I am not able to do anything with submit buttons after opening the dialogue box....please help me on this regard..thanks in advance.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you write that code inside the JSP file or so? You should not write Java code in a JSP file. Java code belongs in a Java class. If it is actually been located in a Java class, then your code flow is simply wrong.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To elaborate on Bauke's correct answer.

JSP is a poor choice of technology for streaming binary data to a client (browser) because it was designed for building up textual responses such as HTML and Javascript. The problems people run into are usually caused by the JSP compiler inserting its own markup and or white space into the response. This causes the response to be returned via a printWriter object instead of a raw servlet output stream.

If you need to return binary data, it's much simpler and cleaner to do so with a servlet.
We have an example application, all packed up in a war and ready to drop into a container and run, in our codeBarn.
See:
http://faq.javaranch.com/java/CodeBarnSimpleStream
 
kumar kiran
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response.sorry guys, i didn't say that i wrote binary code generation in struts action class.I didn't write in the jsp.The control process is when i click the link i am calling a javascript function and submitting the form, then control goes to struts action class there i wrote the code for binary stream.then i am successfully getting the dialogue box.After saving or opening or cancelling ,if i click any submit button in that jsp i am getting the same dialogue box.that's the problem.....
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, this sounds more like an issue on the client side.

I'm going to move this to our HTML forum for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic