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.

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=downloadfilename.csv");

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:\\filename.zip"));
2. Project web path

Assume your file is located at “http://yourname.com/StrutsExample/upload/filename.zip“, and the “StrutsExample” is your project name (serlvet context).

//jndi:/yourname.com/StrutsExample/upload/filename.zip
URL url = getServlet().getServletContext().getResource("upload/filename.zip");
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 – Struts-Download-File-Example.zip

1. Action class

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

package com.mkyong.common.action;
 
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
 
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{
 
   @Override
   public ActionForward execute(ActionMapping mapping, ActionForm form,
     HttpServletRequest request, HttpServletResponse response)
     throws Exception {
 
     //return an application file instead of html page
     response.setContentType("application/octet-stream");
     response.setHeader("Content-Disposition","attachment;filename=superfish.zip");
 
     try 
     {
       	//Get it from file system
       	FileInputStream in = 
      		new FileInputStream(new File("C:\\superfish.zip"));
 
        //Get it from web path
        //jndi:/localhost/StrutsExample/upload/superfish.zip
        //URL url = getServlet().getServletContext()
        //               .getResource("upload/superfish.zip");
        //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(in.read(outputByte, 0, 4096) != -1){
        	out.write(outputByte, 0, 4096);
        }
        in.close();
        out.flush();
        out.close();
 
     }catch(Exception e){
    	e.printStackTrace();
   }
 
   return null;
  }
}

2. JSP Page

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
</head>
<body>
 
Download file from server - 
<html:link action="/DownloadIt">struts-tutorial.zip</html:link> 
 
</body>
</html>

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" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<action-mappings>
 
		<action
			path="/DownloadPage"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/pages/display.jsp"/>
 
		<action
			path="/DownloadIt"
			type="com.mkyong.common.action.DownloadFileAction"
			>
		</action>
 
	</action-mappings>
 
</struts-config>

4. Test it

http://localhost:8080/StrutsExample/DownloadPage.do

struts-download-file-example1
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, 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.

Comments