• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JSP: DownLoad a file

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to give user a download file option. when he clicks: save dialog box has to opened and save the file at given path.

There is a problem with my code:

File fil = new File(file_name);
long len = fil.length();
int leng = (int)len;
byte []fileByte = new byte[leng];
//first read the file on the server, store the contents in a byte array
FileInputStream fis = new FileInputStream(fil);
fis.read(fileByte);
response.setContentLength(leng);
response.setHeader("Content-Disposition", "attachment;filename=file_name");

//write the byte array to the o/p stream.
response.getOutputStream().write(fileByte);
--------------------------------------------------------
I am getting error like: getOutputStream() has already been called for this response.

I think my code won't work for all cases, i need a better solution please.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you not making a link to file in your page??
 
Madhu Kaparapu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting you, can you exaplain clearly

1) test1.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>test1.jsp</title>
</head>
<body>
<a href="fileDownload.jsp">download</a>
</body>
</html>

2) fileDownload.jsp

the code i mentioned earlier

But i am getting the error message like:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Adeel means is that, since you already have the file on your server, why don't you provide a link to that file on your output page? Or don't you want to put it in a publicly accessible place?
Streaming files from a JSP is tricky, as you have found, because JSPs may write data to the OutputStream before your code runs. I'd recommened converting the download-JSP into a servlet; that way it's easier to control the OutputStream.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I meant was like, if I want to give a.zip file to downlaod. I would just make a link to the file a.zip in my jsp page. So the user can download it by clicking the link.

<a href="/a.zip">download</a>
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSPs are designed for formatting text.
If you want to stream binary data, you're better off doing it from a servlet.

Any characters outside of the "<%...%>" tags will result in an out.println(..) statement being placed in the generated servlet code - even a new line or other whitespace character. This will class with your own handling of the output stream.

If the file lives within your webapp, the simplest thing to do is provide a link as Adeel has mentioned. If not, put the stream handling code in a servlet. If you would like an example of this, I have one on http://simple.souther.us. Look for SimpleStream.
 
Madhu Kaparapu
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanku Adeel Ansari

i am successful for what you said.

Regards
Madhu Kumar
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic