Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Download link/button

Posted on 2010-11-20
8
Medium Priority
?
381 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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

877 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