Downloading file via JSP

Hi all,

I'm trying to download file from my JSP. After submitting the form in JAVA code I have put this lines :

response.setContentType("application/download");
response.addHeader("content-disposition", "attachment; filename=TEST.zip");

also I'm sending the file throught PrintWriter, but when I invoke
pw.flush(); the contents of the file is flushed to my browser. Actually I want "Open/Save" dialog to popup.

How to invoke this window?!?!?! How to download file via my JSP?

Thanks in advance!
LVL 16
ValeriAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ldbkuttyCommented:
>> Actually I want "Open/Save" dialog to popup.
http://www.experts-exchange.com/Web/Web_Languages/HTML/Q_21001243.html
0
siliconeagleCommented:
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment; filename="+filename);
0
TimYatesCommented:
> also I'm sending the file throught PrintWriter, but when I invoke

Don't use print writer...that's for Text only...  Send it down the response.getOutputStream()
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ValeriAuthor Commented:
When I invoke "response.getOutputStream()" I'm getting exception
getWriter() is already invoked for this response, or something like that!
But if I have the file, way I need to send it through ServletOutputStream?!?! This is something that I cannot understand and probably my problems come because of this?!?!
0
TimYatesCommented:
Can you post your JSP code?
0
ValeriAuthor Commented:
Let say that in directory "c:/test/" I have file test.zip or test.txt. How to show Open/Save dialog for this file?
0
ValeriAuthor Commented:
if (jspAction != null && jspAction.equals(ACTION_DOWNLOAD)) {

        File fFile = new File(strfilepath);

        InputStream in = null;
        PrintWriter sosStream = null;
        try {
          in = new FileInputStream(fFile);
          sosStream = new PrintWriter(out);

          byte[] buf = new byte[4 * 1024];  // 4K buffer
          int bytesRead;
          while ((bytesRead = in.read(buf)) != -1) {
            sosStream.write(new String(buf), 0, bytesRead);
          }
          sosStream.flush();
        }
        finally {
          if (in != null) in.close();
        }
    }
0
TimYatesCommented:
Is that your entire JSP?

You cannot send anything (<HEAD> for example) before this will work:

-----------------
<%
    if (jspAction != null && jspAction.equals(ACTION_DOWNLOAD))
    {
        File fFile = new File(strfilepath);
        BufferedInputStream in = null;
        BufferedOutputStream sosStream = null ;
        try
        {
          response.setContentType( "application/octet-stream" );
          response.setHeader( "Content-Disposition", "attachment; filename="+ strfilepath );
          in = new BufferedInputStream( new FileInputStream( fFile ) ) ;
          sosStream = new BufferedOutputStream( response.getOutputStream() ) ;

          byte[] buf = new byte[ 4 * 1024 ];  // 4K buffer
          int bytesRead;
          while( ( bytesRead = in.read( buf ) ) != -1 )
          {
            sosStream.write( buf, 0, bytesRead ) ;
          }
          sosStream.flush();
        }
        catch( IOException ex )
        {
            ex.printStackTrace() ;
        }
        finally
        {
          try { if( in != null) in.close(); } catch( IOException ex ) { }
          try { if( sosStream != null) sosStream.close(); } catch( IOException ex ) { }
        }
    }
    else
    {
%>
<HTML>
    <HEAD>
        <TITLE>Nothing to do</TITLE>
    </HEAD>
</HTML>
<%
    }
%>
0
ValeriAuthor Commented:
No, it was only part of my JSP code...
I tryed with BufferedOutputStream, but when I invoke response.getOutputStream(), exception

<java.lang.IllegalStateException: getWriter() has already been called for this response> is thrown.

In code that I posted "out" is something of type JspWriter and I don't know where it comes from, because I'm not so familiar with JSP's and the code that I'm working with is not mine. And I have no idea way response.getOutputStream() throws this exception?!?! Probably your code will work, but I can't try it because of this exception...:-(
0
TimYatesCommented:
You get that exception, because you have already displayed a bit of HTML to the browser, and it cannot change what it is sending (or send 2 different things) on the same response...

This will have to be a seperate JSP

What people usually do, is have a page which says "download will start in 5 seconds...click here if it doesn't"

Then put a META REFRESH tag at the top of that page, to redirect to your download page in 5 seconds, and add a A HREF link to the same page (for people who don't have browsers that support META REFRESH...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ValeriAuthor Commented:
It doesn't looks so easy...;-)
I decided to generate the file and to point to this file by <a></a>. But  I'm face another problem, how to point to this file in right way. On my PC it works, because the file is placed localy, but I want the link to be something like that :
href="x.x.x.x/path to the file...etc".
How to set this link corectly?!
0
TimYatesCommented:
copy the file into the webapplication directory...ie:  with tomcat:

/tomcat/webapps/YOURAPP/downloads

then link to "downloads/file.zip"

>  It doesn't looks so easy...;-)

It's not too bad...  just have a jsp which takes a parameter (to build the file), and then send this file to the response.getOutputStream rather than a FileOutputStream

It's just the jsp cannot show some HTML AND send the file...it can only do 1 thing, so you need 2 jsps
0
ValeriAuthor Commented:
I didn't understand how to point to this file... :-(
I'm not so familiar with JSP's and with Tomcat as well. My Tomcat is installed in Program Files/Tomcat 4.1. There is "webapps" directory, but there is not subdirectory "MYAPP".
Currently, when I'm creating the file it is placed in Tomcat/bin directory?!?!
Do I have to create "/YOURAPP/downloads" directory?
Where is the root directory right now, under process of development? Where it will be after deployment?
BTW thanks for your help and patience!
0
TimYatesCommented:
Are you putting your JSPs in webapps/ROOT  ?

Then put the files inside a folder webapps/ROOT/downloads

And you should be able to link to them as

<a href="downloads/filename.zip">here</a>
0
TimYatesCommented:
you will have to create the downloads directory :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.