Solved

Download link/button

Posted on 2010-11-20
8
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
A solution for Fortify Path Manipulation.
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.
Suggested Courses

624 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