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>ADS_TO_REPLACE_1

<head>

<title>File Upload Interceptor Example</title>

</head>ADS_TO_REPLACE_2

<body>

<h1>File Upload Interceptor Example</h1>

<hr />ADS_TO_REPLACE_3

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

</body>

</html>

2-fileupload.jsp ADS_TO_REPLACE_4

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

<html>

<head>ADS_TO_REPLACE_5

<title>File Upload Interceptor Example</title>

</head>

<body>ADS_TO_REPLACE_6

<h1>File Upload Interceptor Example</h1>

<hr />

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

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

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

</s:file>ADS_TO_REPLACE_8

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

</s:form>

</body>ADS_TO_REPLACE_9

</html>

 

3-FileUploadAction.java

package roseindia.action;

import java.io.File;ADS_TO_REPLACE_10

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

private File Uploadfile;ADS_TO_REPLACE_11

private String UploadfileFileName;

private String UploadfileContentType;

public File getUploadfile() {ADS_TO_REPLACE_12

return Uploadfile;

}

public void setUploadfile(File uploadfile) {ADS_TO_REPLACE_13

Uploadfile = uploadfile;

}

public String getUploadfileFileName() {ADS_TO_REPLACE_14

return UploadfileFileName;

}

public void setUploadfileFileName(String uploadfileFileName) {ADS_TO_REPLACE_15

UploadfileFileName = uploadfileFileName;

}

public String getUploadfileContentType() {ADS_TO_REPLACE_16

return UploadfileContentType;

}

public void setUploadfileContentType(String uploadfileContentType) {ADS_TO_REPLACE_17

UploadfileContentType = uploadfileContentType;

}

public String execute() throws Exception {ADS_TO_REPLACE_18

return SUCCESS;

}

public String uploadForm() {ADS_TO_REPLACE_19

return NONE;

}

}

4_struts.xml

<?xml version="1.0" encoding="UTF-8" ?>ADS_TO_REPLACE_20

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">ADS_TO_REPLACE_21

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

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

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

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

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

method="uploadForm">

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

</action>ADS_TO_REPLACE_24

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

<interceptor-ref name="i18n" />

<interceptor-ref name="fileUpload">ADS_TO_REPLACE_25

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

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

</interceptor-ref>ADS_TO_REPLACE_26

<interceptor-ref name="params">

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

</interceptor-ref>ADS_TO_REPLACE_27

<interceptor-ref name="validation">

</interceptor-ref>

<interceptor-ref name="workflow">ADS_TO_REPLACE_28

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

</interceptor-ref>

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

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

</action>

</package>ADS_TO_REPLACE_30

</struts>

5_fileUploadSuccess.jsp

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

<html>

<head>ADS_TO_REPLACE_31

<title>File Upload Interceptor Example</title>

</head>

<body>ADS_TO_REPLACE_32

<h1>File Upload Interceptor Example</h1>

<hr />

File path :ADS_TO_REPLACE_33

<s:property value="Uploadfile" />

<br><br><br>

File name :ADS_TO_REPLACE_34

<s:property value="UploadfileFileName" />

<br><br><br>

File type :ADS_TO_REPLACE_35

<s:property value="UploadfileContentType" /> 

</body>

</html>

6_struts-messages.properties

struts.messages.error.uploading - File uploading failedADS_TO_REPLACE_36

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.gifADS_TO_REPLACE_37

fileUploadForm.gif

ADS_TO_REPLACE_38

/tutorialfiles/29045.UploadedFileInfo.gif

Download Select Source CodeADS_TO_REPLACE_39

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

Advertisements

Ads

Ads

 
Advertisement null

Ads