Tuesday, 19 January 2016

Struts2: Upload a file example

In this post, I am going to explain how to upload a file using struts2 framework. Struts2 provides fileUpload interceptor to upload files. By default it is part of default interceptor stack. fileUpload interceptor stack provides following parameters, by using these you can control the size of the file to upload, types of files to upload.

Property
Description
maximumSize
the maximum size in bytes. Default size is 2MB.
allowedTypes
a comma separated list of content types, image/png,image/gif,image/jpeg,application/pdf.
allowedExtensions
a comma separated list of file extensions (ie: .html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all extensions to be uploaded.


Example
<action name="doUpload" class="com.sample.UploadAction">
    <interceptor-ref name="fileUpload">
    <param name="allowedTypes">image/png,image/gif,image/jpeg,application/pdf</param>
    <param name="maximumSize">1024000</param>
 </interceptor-ref>
    <interceptor-ref name="basicStack"/>
    <result name="success">good_result.jsp</result>
</action>

Following table summarizes the files; I am going to use to develop working application.
File
Description
UploadAction.java
Upload a file, return “success” on successful upload of file.
success.jsp
You will get this page once the file is uploaded successfully
index.jsp
HTML form to upload a file.

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

Step 1: Create new dynamic web project struts2_file_upload, in eclipse.
File -> New -> Dynamic Web project.

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

Step 3 : Update pom.xml for maven dependencies. I am going to use following dependencies.
pom.xml
<dependency>
 <groupId>org.apache.struts</groupId>
 <artifactId>struts2-core</artifactId>
 <version>2.3.20</version>
</dependency>

<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-io</artifactId>
 <version>1.3.2</version>
</dependency>


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>zimply</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <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 5: Create index.jsp and add following content. 
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>file upload page</title>
</head>
<body>
 <h3>Struts 2 file upload example</h3>
 <s:form action="doUpload" method="post" enctype="multipart/form-data">
  <s:file name="document" label="Select the file to upload" />
  <s:submit value="upload" align="center" />
 </s:form>
</body>
</html>


Step 6: Define success.jsp like below.
success.jsp    
<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>Upload Successful</title>
<s:head />
</head>
<body>

 <h3>File Uploaded successfully</h3>
 File Name :
 <s:property value="documentFileName"></s:property>
 <br /> Content type:
 <s:property value="documentContentType"></s:property>
 <br /> User file :
 <s:property value="document"></s:property>
</body>
</html>


Step 7: Create package com.sample, define UploadAction.java like below.

UploadAction.java

package com.sample;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
 private File document;
 private String documentFileName;
 private String documentContentType;

 public String execute() {
  String targetPath = "/Users/harikrishna_gurram/temp";
  File fileToCreate = new File(targetPath, documentFileName);
  try {
   FileUtils.copyFile(this.document, fileToCreate);
  } catch (IOException e) {
   addActionError(e.getMessage());
  }
  return "success";
 }

 public File getDocument() {
  return document;
 }

 public void setDocument(File document) {
  this.document = document;
 }

 public String getDocumentFileName() {
  return documentFileName;
 }

 public void setDocumentFileName(String documentFileName) {
  this.documentFileName = documentFileName;
 }

 public String getDocumentContentType() {
  return documentContentType;
 }

 public void setDocumentContentType(String documentContentType) {
  this.documentContentType = documentContentType;
 }

}


fileUpload automatically set the request parameters documentFileName, documentContentType based on the document. Suppose [File Name] is the actual file, then [File Name]ContentType represent the content type of the file, [File Name]FileName represent the actual name of the file uploaded.

Step 8: 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="doUpload" class="com.sample.UploadAction">
   <interceptor-ref name="fileUpload">
    <param name="allowedTypes">image/png,image/gif,image/jpeg,application/pdf</param>
    <param name="maximumSize">1024000</param>
   </interceptor-ref>
   <interceptor-ref name="validation">
    <param name="excludeMethods">input,back,cancel,browse</param>
   </interceptor-ref>
   <interceptor-ref name="workflow">
    <param name="excludeMethods">input,back,cancel,browse</param>
   </interceptor-ref>
   <interceptor-ref name="basicStack"/>
   <result name="success">/success.jsp</result>
   <result name="input">/index.jsp</result>
  </action>

 </package>

</struts>


Observe struts.xml, I set maximumSize to 10MB (1024000 bytes), and  allowed types to image/png,image/gif,image/jpeg,application/pdf. If user tries to upload any file > 10MB (or) invalid format, struts2 return index.jsp page by setting field errors.

Field errors are set by field interceptor automatically.
This interceptor will add several field errors, assuming that the action implements ValidationAware. These error messages are based on several i18n values stored in struts-messages.properties, a default i18n file processed for all i18n requests. You can override the text of these messages by providing text for the following keys:

Key
Description
struts.messages.error.uploading
A general error that occurs when the file could not be uploaded
struts.messages.error.file.too.large
occurs when the uploaded file is too large
struts.messages.error.content.type.not.allowed
occurs when the uploaded file does not match the expected content types specified
struts.messages.error.file.extension.not.allowed
occurs when the uploaded file does not match the expected file extensions specified

Following is the complete project structure.
Run the project on server, you will get following screen.
If you upload valid file, you will get following kind of output.

If you upload any file > 10MB size, (or) in valid format file, you will get following kind of output.

References







Previous                                                 Next                                                 Home

No comments:

Post a Comment