Struts – download file from website example

To let user download a file from your Struts web project, you have to inform “HttpServletResponse” to return an application file instead of the normal HTML page.


In server side, you can get the download file from 3 different ways

1. File system

Get it from file system

FileInputStream in = 
  new FileInputStream(new File("C:\\"));
2. Project web path

Assume your file is located at ““, and the “StrutsExample” is your project name (serlvet context).

URL url = getServlet().getServletContext().getResource("upload/");
InputStream in = url.openStream();
3. Bytes array

If you retrieve the files or blob data from database, it’s usually return as bytes array.

byte[] bytes = new byte[4096];
InputStream in = new ByteArrayInputStream(bytes);
Download this Struts download file example –

1. Action class

An action class to return an application file instead of the normal HTML page, and get the “” file for user to download.

package com.mkyong.common.action;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class DownloadFileAction extends Action{
   public ActionForward execute(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response)
     throws Exception {
     //return an application file instead of html page
       	//Get it from file system
       	FileInputStream in = 
      		new FileInputStream(new File("C:\\"));
        //Get it from web path
        //URL url = getServlet().getServletContext()
        //               .getResource("upload/");
        //InputStream in = url.openStream();
        //Get it from bytes array
        //byte[] bytes = new byte[4096];
        //InputStream in = new ByteArrayInputStream(bytes);
        ServletOutputStream out = response.getOutputStream();
        byte[] outputByte = new byte[4096];
        //copy binary content to output stream
        while(, 0, 4096) != -1){
        	out.write(outputByte, 0, 4096);
     }catch(Exception e){
   return null;

2. JSP Page

<%@taglib uri="" prefix="html"%>
Download file from server - 
<html:link action="/DownloadIt"></html:link> 

3. struts-config.xml

Struts configuration file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 

4. Test it


Tags :

About the Author

Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.


  • Pingback: j bradley electrician()

  • Pingback: electrician jobs nyc()

  • Pingback: house blue()

  • Pingback: Looid()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: numerous plumbing companies()

  • Pingback: plumbers o rings uk()

  • Pingback: locksmiths in memphis tn()

  • Pingback: a locksmith casselberry fl()

  • Pingback: electricians killeen tx()

  • Pingback: click for more info()

  • Pingback: pay per day loan plans()

  • Pingback: water ionizer()

  • Pingback: alkaline water brands()

  • Pingback: car parking()

  • Pingback: alkaline water machine()

  • Pingback: hurtigt laan penge nu()

  • Pingback: water ionizer()

  • Pingback: car parking()

  • Pingback: bedste laan lige nu()

  • Pingback: mobile porn movies()

  • Pingback: Click Here()

  • Pingback: TV options for restaurants()

  • Pingback: online casinos()

  • Pingback: vpn service()

  • Pingback: kangen water machine()

  • Pingback: alkaline water()

  • Pingback: water ionizer()

  • Pingback: free movie downloads()

  • Pingback: watch movies online()

  • Pingback: stream movies()

  • Pingback: Blue Coaster33()

  • Arne

    Very nice article; helped me a long way. Thanks

  • Yolanda Olmedo Canabal

    Excelente Post!! funciona de maravilla. Gracias!

  • prince

    hi sir i want to store a file in database and i want to retrieve the stored file from the database and the i should get listed all the stored file from the database and among those i should select any one and the selected file should get downloaded in my local machine i want to do it in struts please provide me solution for this….

  • sriram

    iam using struts 1.x and i tried the code here for downloading the file,i write the same code in Struts action class.after that file is downloading properly,but it is not forwarding to another page.i write the code at the end as return mapping.findForward(“someText”);

    and in struts-config.xml file i mention the path associated with this name sir.but it is not forwarding

  • Kane

    Hi ,
    I have tried this code . I am picking up Excel file from path on disk .
    It only works with Firefox, chrome , safari but has problem with IE 8 .

    Please help with any suggestion .
    My code snippet :

    String disHeader = “attachment; filename=”;
    ServletOutputStream opstream = response.getOutputStream();

    response.setContentType( “application/x-excel” );
    response.setHeader( “content-disposition”,disHeader+ fileName );

    byte[] buf = new byte[4096];

    while( (,0,4096)) != -1 )
    //byte[] defaultBytes = (new String(buf)).getBytes(“GBK”);
    // String newEncodingStr = new String(defaultBytes, “UTF-8″);


  • mahesh

    How to retrieve a file from database based on search content in struts

  • Godfray

    how can i refresh the jsp page again?
    if i use
    return mapping.findForward(“success”);
    instead of
    return null;,
    it gives error that response already….

  • Vipin

    Sir Your Code was very Good….
    it really Works Well.

    Thanks a lot Sir.

  • Shalini

    Sir i wann to know that how i implement autocomplete off in html tag in struts please answer it.

  • Siddharth Mehta

    how can i extend this code to run in struts 2?

  • candida

    I am glad to find so various helpful information here within the post, we need develop additional techniques in this regard, thanks for sharing

  • Best Man Gifts

    Best Man Gifts

    As a man, you score to permit, you most likely possess never been just at shopping, or justified likeable shopping for that entity. Now that you’re achievement to be married, you’ll feat that choosing gifts for your optimum man bonk turn voice of your to-do itemise. It’s a stimulating task, but if you see where to looking, you’ll be fit to feat the perfect sharing in no abstraction.

  • free manga download A neater examine Marvel comics online for free?

  • Pingback: Struts 2 download file example()

  • Pingback: how to buy Xanax online without a prescription, buy Xanax with no … | Rx Drugs Blog()