Home Struts Struts Struts2.2.1 Tags Struts2.2.1 file upload example.

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 file upload example.
Posted on: January 12, 2011 at 12:00 AM
Advertisement
In this example, you will see the use of struts2.2.1 file tag.

Struts2.2.1 file upload example.

In this example, you will see how to upload file in struts2.2.1. Here, we are using a struts2.2.1 file tag  for uploading a file. Struts2.2.1 utilizes the service of File Upload Interceptor to add the support for uploading files in the Struts applications. 

Directory structure of file tag example.

 1- index.html

<html>

<head><title>Struts2.2.1_file_Tag_Example</title></head>

<body>

<h1>Struts2.2.1_file_Tag_Example</h1><hr/>

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

</body>

</html>

2-fileupload.jsp

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

<html>

<head><title>Struts2.2.1_file_Tag_Example</title></head>

<body>

<h1>Struts2.2.1_file_Tag_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

<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">200</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>Struts2.2.1_file_Tag_Example</title></head>

<body >

<h1>Struts2.2.1_file_Tag_Example</h1><hr/><h4>

File path :</h4> <s:property value="Uploadfile" /><br/>

<h4>File name :</h4> <s:property value="UploadfileFileName"/><br/>

<h4>File type :</h4><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

SelectRightFormatFile.gif

UploadedFileInfo.gif

Download Select Source Code


Related Tags for Struts2.2.1 file upload example.:


DMCA.com