Struts2.2.1 file upload Interceptor example.
Posted on: January 29, 2011 at 12:00 AM
In this example, we will disscuss about the file Upload Interceptor.

Struts2.2.1 file upload Interceptor example.

In this example, we will disscuss about the file Upload Interceptor. Here, we are using a struts2.2.1 file tag  for uploading a file. Struts2.2.1 utilizes the services of File Upload Interceptor to add the support for uploading files in the Struts applications. 

Directory structure of fileUpload interceptor  example.

 1- index.html

<html>

<head>

<title>File Upload Interceptor Example</title>

</head>

<body>

<h1>File Upload Interceptor Example</h1>

<hr />

<a href="uploadFileAction.action">File Upload Interceptor Example</a>

</body>

</html>

2-fileupload.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<html>

<head>

<title>File Upload Interceptor Example</title>

</head>

<body>

<h1>File Upload Interceptor Example</h1>

<hr />

<s:form action="fileUploadAction.action" method="post"

enctype="multipart/form-data" namespace="/">

<s:file name="Uploadfile" label="Upload file : ">

</s:file>

<s:submit label="Submit"></s:submit>

</s:form>

</body>

</html>

 

3-FileUploadAction.java

package roseindia.action;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

private File Uploadfile;

private String UploadfileFileName;

private String UploadfileContentType;

public File getUploadfile() {

return Uploadfile;

}

public void setUploadfile(File uploadfile) {

Uploadfile = uploadfile;

}

public String getUploadfileFileName() {

return UploadfileFileName;

}

public void setUploadfileFileName(String uploadfileFileName) {

UploadfileFileName = uploadfileFileName;

}

public String getUploadfileContentType() {

return UploadfileContentType;

}

public void setUploadfileContentType(String uploadfileContentType) {

UploadfileContentType = uploadfileContentType;

}

public String execute() throws Exception {

return SUCCESS;

}

public String uploadForm() {

return NONE;

}

}

4_struts.xml

<?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.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />

<constant name="struts.custom.i18n.resources" value="struts-messages" />

<package name="roseindia" extends="struts-default" namespace="/">

<action name="uploadFileAction" class="roseindia.action.FileUploadAction"

method="uploadForm">

<result name="none">jsp/fileupload.jsp</result>

</action>

<action name="fileUploadAction" class="roseindia.action.FileUploadAction">

<interceptor-ref name="i18n" />

<interceptor-ref name="fileUpload">

<param name="allowedTypes">text/html</param>

<param name="maximumSize">10240</param>

</interceptor-ref>

<interceptor-ref name="params">

<param name="excludeParams">dojo\..*,^struts\..*</param>

</interceptor-ref>

<interceptor-ref name="validation">

</interceptor-ref>

<interceptor-ref name="workflow">

<param name="excludeMethods">input,back,cancel,browse</param>

</interceptor-ref>

<result name="success">jsp/fileUploadSuccess.jsp</result>

<result name="input">jsp/fileupload.jsp</result>

</action>

</package>

</struts>

5_fileUploadSuccess.jsp

<%@taglib uri="/struts-tags" prefix="s"%>

<html>

<head>

<title>File Upload Interceptor Example</title>

</head>

<body>

<h1>File Upload Interceptor Example</h1>

<hr />

File path :

<s:property value="Uploadfile" />

<br><br><br>

File name :

<s:property value="UploadfileFileName" />

<br><br><br>

File type :

<s:property value="UploadfileContentType" /> 

</body>

</html>

6_struts-messages.properties

struts.messages.error.uploading - File uploading failed

struts.messages.error.file.too.large - Given file is too large

struts.messages.error.content.type.not.allowed- Please enter contentType file(HTML,txt,Java)

struts.messages.error.file.extension.not.allowed - file extension is not allow

indexJsp.gif

fileUploadForm.gif

/tutorialfiles/29045.UploadedFileInfo.gif

Download Select Source Code

Related Tags for Struts2.2.1 file upload Interceptor example.:

Advertisements

Ads

Ads

 
Advertisement null

Ads