Solved

Download link/button

Posted on 2010-11-20
8
376 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:ganeshaneesh
ID: 34182955
*** Thanks for the response, is this code applicable in execte method of action?
-Aneesh.
0
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

808 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