Monday 25 January 2016

Struts2: Download file example

By setting result type to stream, you can download a file using struts2.

For example,
<result name="success" type="stream">
  <param name="contentType">image/jpeg</param>
  <param name="inputName">imageStream</param>
  <param name="contentDisposition">attachment;filename="document.pdf"</param>
  <param name="bufferSize">1024</param>
</result>

Following are the parameters for result of type stream.
Parameter
Description
contentType
MIME type of the data. Following link shows al the available MIME types.
contentLength
Stream lenght in bytes.
contentDisposition
Specifies the file name, default = inline, values are typically attachment;filename="document.pdf".
inputName
The name of the InputStream property from the chained action (default = inputStream)
bufferSize
Size of buffer, defuult 1024
allowCaching
if set to 'false' it will set the headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from caching the content. (default = true)
contentCharSet
if set to a string, ';charset=value' will be added to the content-type header, where value is the string set. If set to an expression, the result of evaluating the expression will be used. If not set, then no charset will be set on the header

Following step-by-step procedure explains, how to develop complete working application in Eclipse.

Step 1: Create new dynamic web project struts_file_download.
File -> New -> Dynamic Web Project.

Step 2: Mavenize the project. Right click on the project -> Configure -> Convert to Maven Project.


Update pom.xml for maven dependencies.
<dependency>
 <groupId>org.apache.struts</groupId>
 <artifactId>struts2-core</artifactId>
 <version>2.3.20</version>
</dependency>


Step 3: Create package com.sample.actions, define the class DownloadAction.java

DownloadAction.java

package com.sample.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends ActionSupport {

 private InputStream fileInputStream;
 private String fileToDownload = "/Users/harikrishna_gurram/event/1/notes.docx";
 private String fileName;

 public InputStream getFileInputStream() {
  fileName = "notes.docx";
  return fileInputStream;
 }

 public String execute() throws Exception {
  fileInputStream = new FileInputStream(new File(fileToDownload));
  return SUCCESS;
 }

 public String getFileName() {
  return fileName;
 }

}


Step 4: Update web.xml like below.
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <display-name>struts_action_messages</display-name>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
 <display-name>struts action messages example</display-name>
 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <session-config>
  <session-timeout>1</session-timeout>
 </session-config>

</web-app>


Step 7: It is time to create struts.xml. Since Struts 2 requires struts.xml to be present in classes folder. Create struts.xml file under the WebContent/WEB-INF/classes folder. Eclipse does not create the "classes" folder by default, so you need to do this yourself. To do this, right click on the WEB-INF folder in the project explorer and select New > Folder. Create struts.xml file inside classes.

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
 <package name="default" extends="struts-default">
  <action name="download" class="com.sample.actions.DownloadAction">
   <result name="success" type="stream">
    <param name="contentType">application/octet-stream</param>
    <param name="inputName">fileInputStream</param>
    <param name="contentDisposition">attachment;filename="${fileName}"</param>
    <param name="bufferSize">1024</param>
   </result>
  </action>
 </package>

</struts>


I set contentType to "application/octet-stream", it represents Binary Data.

Total project structure looks like below.

Run the application on server and hit following url to download file ‘notes.docx’. 


References









Previous                                                 Next                                                 Home

No comments:

Post a Comment