Solved

Download link/button

Posted on 2010-11-20
8
374 Views
Last Modified: 2012-08-13
Hi,

I have deployed a struts application on tomcat server on linux box. As part of some operatio, it creates some files(.xls), in /usr/local directly. Now from the JSP, i have to give a link/button to the user to donwload the xls file. Please help, idea and logic.

Thanks,
Aneesh.
0
Comment
Question by:ganeshaneesh
  • 4
  • 4
8 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 300 total points
ID: 34180919
You can have the button make a request to a servlet. Here is an example code. You could have the JSP send the name of the file in the query string of the url. The servlet could use the getParameter method to assign it to filename.
package rrz;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DownloadServlet extends HttpServlet {
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws IOException, ServletException
        {
             InputStream in = null;
             OutputStream out = null;
             try {
                 //// File file = new File("/usr/local/test.xls");
                  File file = new File(getServletContext().getRealPath("/test.xls"));
                  String filename = "test.xls";
                  FileInputStream fis = new FileInputStream(file);
                  response.setContentType("application/vnd.ms-excel");
                  ServletOutputStream sos = response.getOutputStream();
                  out = new BufferedOutputStream(response.getOutputStream());
                  in = new BufferedInputStream(fis);
                  response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
                  int c; 
                  while( ( c=in.read() ) != -1 ) out.write( c );
             }
             catch(FileNotFoundException fnfe){
                  response.sendError( HttpServletResponse.SC_NOT_FOUND );
             }
             finally {
               if( in != null ) try { in.close(); } catch( Exception e ) {}
               if( out != null ) try { out.close(); } catch( Exception e ) {}
             }
     }
}

Open in new window

0
 
LVL 27

Expert Comment

by:rrz
ID: 34180925
oops please delete my file line and uncomment the commented one
0
 

Author Comment

by:ganeshaneesh
ID: 34182951
Thanks for the response, is this code will be application in execte method of action?
-Aneesh.
0
 

Author Comment

by:ganeshaneesh
ID: 34182955
*** Thanks for the response, is this code applicable in execte method of action?
-Aneesh.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 27

Expert Comment

by:rrz
ID: 34183429
>is this code applicable in execte method of action?
I don't know what are asking here.  
For a link, you could use the following in your JSP.  
<a href="http://yourserver.com/download">download file</a>

Open in new window

 
This assumes that you have added the following to your web app's web.xml file.
     <servlet>      
                   <servlet-name>download</servlet-name>
                   <servlet-class>rrz.DownloadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
                    <servlet-name>download</servlet-name>
                    <url-pattern>/download</url-pattern>
    </servlet-mapping>  

Open in new window

0
 

Author Comment

by:ganeshaneesh
ID: 34183649
Fine, actually I would like to if thise code will work in action sevlet?
-Aneesh.
0
 
LVL 27

Expert Comment

by:rrz
ID: 34183812
I don't use Struts. So, I posted an example that doesn't use it. I see that Struts has the  DownloadAction object. Look at
http://wiki.apache.org/struts/StrutsFileDownload  
Or you could use Action class. See  
http://www.mkyong.com/struts/struts-download-file-from-website-example/
0
 

Author Comment

by:ganeshaneesh
ID: 34186598
I put the above servlet code in execute method of my Action class, got it working.
Thanks,
Aneesh.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now