[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

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!
0
Valeri
Asked:
Valeri
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now