Upload File using in struts 2
-Use struts2 required lib jar files and see the following files also...
1) Here is the action file....
FileUploadAction.java
------------------------------------------
package com.example.common.action;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
public String execute() throws Exception{
return SUCCESS;
}
public void validate(){
if(getFileUpload() !=null){
System.out.println("======File size validation before upload: size in bytes: "+getFileUpload().length());
if(getFileUpload().length()>202400){ //Give the size in bytes whatever you want to take
addActionError("File is too large ! Select less than 200Kb file");
}else{
addActionMessage("File Uploaded successfully!");
}
}
}
public String display() {
return NONE;
}
}
-----------------------------------------------------
2) Here is the UI (Jsp files)
FileUpload.jsp
-------------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<style type="text/css">
.welcome {
background-color:#DDFFDD;
border:1px solid #009900;
width:200px;
}
.welcome li{
list-style: none;
}
.errors {
background-color:#FFCCCC;
border:1px solid #CC0000;
width:400px;
margin-bottom:8px;
}
.errors li{
list-style: none;
}
</style>
<h1>Struts 2 file upload example</h1>
<s:if test="hasActionMessages()">
<div class="welcome">
<s:actionmessage/>
</div>
</s:if>
<s:elseif test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div>
</s:elseif>
<s:form action="resultAction" namespace="/" method="POST" enctype="multipart/form-data">
<s:file name="fileUpload" label="Select a File to upload" size="40" />
<s:submit value="submit" name="submit" />
</s:form>
-----------------------------------------------
Result.jsp
--------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<style type="text/css">
.welcome {
background-color:#DDFFDD;
border:1px solid #009900;
width:200px;
}
.welcome li{
list-style: none;
}
.errors {
background-color:#FFCCCC;
border:1px solid #CC0000;
width:400px;
margin-bottom:8px;
}
.errors li{
list-style: none;
}
</style>
<h1>Struts 2 file upload example</h1>
<s:if test="hasActionMessages()">
<div class="welcome">
<s:actionmessage/>
</div>
</s:if>
<s:elseif test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div>
</s:elseif>
Uploaded File Name : <s:property value="fileUploadFileName"/>
Content Type : <s:property value="fileUploadContentType"/>
------------------------------------------------------------------
3) Properties files under src folder..
global.properties
--------------------------
#Global messages
global.username = Username
global.password = Password
global.submit = Submit
global.reset = Reset
global.userImage= Upload File
global.description= Description
#struts.messages.error.uploading - File upload failed
#struts.messages.error.file.too.large - uploaded file is too large
#struts.messages.error.content.type.not.allowed - file content type is not allow
#struts.messages.error.file.extension.not.allowed - file extension is not allow
------------------------------------------------------------------------------
4) struts.xml under src folder......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="false"/>
<package name="default" namespace="/" extends="struts-default">
<action name="fileUploadAction" class="com.example.common.action.FileUploadAction" method="display">
<result name="none">fileupload.jsp</result>
</action>
<action name="resultAction" class="com.example.common.action.FileUploadAction">
<interceptor-ref name="exception"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/gif,image/png,image/pjpeg</param>
<param name="maximumSize">1097152</param>
</interceptor-ref>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</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>
<result name="success">Result.jsp</result>
<result name="input">FileUpload.jsp</result>
</action>
</package>
</struts>
6) web.xml under WEB-INF/classes folder
----------------------------------------------------------
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Struts 2 Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>FileUpload.jsp</welcome-file>
</welcome-file-list>
</web-app>
----------------------------------------------------------------
See the screen shots.......
1) Here is the action file....
FileUploadAction.java
------------------------------------------
package com.example.common.action;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport{
private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;
public String getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
public File getFileUpload() {
return fileUpload;
}
public void setFileUpload(File fileUpload) {
this.fileUpload = fileUpload;
}
public String execute() throws Exception{
return SUCCESS;
}
public void validate(){
if(getFileUpload() !=null){
System.out.println("======File size validation before upload: size in bytes: "+getFileUpload().length());
if(getFileUpload().length()>202400){ //Give the size in bytes whatever you want to take
addActionError("File is too large ! Select less than 200Kb file");
}else{
addActionMessage("File Uploaded successfully!");
}
}
}
public String display() {
return NONE;
}
}
-----------------------------------------------------
2) Here is the UI (Jsp files)
FileUpload.jsp
-------------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<style type="text/css">
.welcome {
background-color:#DDFFDD;
border:1px solid #009900;
width:200px;
}
.welcome li{
list-style: none;
}
.errors {
background-color:#FFCCCC;
border:1px solid #CC0000;
width:400px;
margin-bottom:8px;
}
.errors li{
list-style: none;
}
</style>
<h1>Struts 2 file upload example</h1>
<s:if test="hasActionMessages()">
<div class="welcome">
<s:actionmessage/>
</div>
</s:if>
<s:elseif test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div>
</s:elseif>
<s:form action="resultAction" namespace="/" method="POST" enctype="multipart/form-data">
<s:file name="fileUpload" label="Select a File to upload" size="40" />
<s:submit value="submit" name="submit" />
</s:form>
-----------------------------------------------
Result.jsp
--------------------------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<style type="text/css">
.welcome {
background-color:#DDFFDD;
border:1px solid #009900;
width:200px;
}
.welcome li{
list-style: none;
}
.errors {
background-color:#FFCCCC;
border:1px solid #CC0000;
width:400px;
margin-bottom:8px;
}
.errors li{
list-style: none;
}
</style>
<h1>Struts 2 file upload example</h1>
<s:if test="hasActionMessages()">
<div class="welcome">
<s:actionmessage/>
</div>
</s:if>
<s:elseif test="hasActionErrors()">
<div class="errors">
<s:actionerror/>
</div>
</s:elseif>
Uploaded File Name : <s:property value="fileUploadFileName"/>
Content Type : <s:property value="fileUploadContentType"/>
------------------------------------------------------------------
3) Properties files under src folder..
global.properties
--------------------------
#Global messages
global.username = Username
global.password = Password
global.submit = Submit
global.reset = Reset
global.userImage= Upload File
global.description= Description
#struts.messages.error.uploading - File upload failed
#struts.messages.error.file.too.large - uploaded file is too large
#struts.messages.error.content.type.not.allowed - file content type is not allow
#struts.messages.error.file.extension.not.allowed - file extension is not allow
------------------------------------------------------------------------------
4) struts.xml under src folder......
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="global" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<constant name="struts.devMode" value="false"/>
<package name="default" namespace="/" extends="struts-default">
<action name="fileUploadAction" class="com.example.common.action.FileUploadAction" method="display">
<result name="none">fileupload.jsp</result>
</action>
<action name="resultAction" class="com.example.common.action.FileUploadAction">
<interceptor-ref name="exception"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/gif,image/png,image/pjpeg</param>
<param name="maximumSize">1097152</param>
</interceptor-ref>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*</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>
<result name="success">Result.jsp</result>
<result name="input">FileUpload.jsp</result>
</action>
</package>
</struts>
6) web.xml under WEB-INF/classes folder
----------------------------------------------------------
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Struts 2 Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>FileUpload.jsp</welcome-file>
</welcome-file-list>
</web-app>
----------------------------------------------------------------
See the screen shots.......
Comments
Post a Comment