Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Download link/button

Posted on 2010-11-20
8
Medium Priority
?
380 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 28

Accepted Solution

by:
rrz earned 1200 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 28

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

 

Author Comment

by:ganeshaneesh
ID: 34182955
*** Thanks for the response, is this code applicable in execte method of action?
-Aneesh.
0
 
LVL 28

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 28

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

Industry Leaders: 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.

670 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