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>ADS_TO_REPLACE_1 <head><title>Struts2.2.1_file_Tag_Example</title></head> <body> <h1>Struts2.2.1_file_Tag_Example</h1><hr/>ADS_TO_REPLACE_2 <a href="uploadFileAction.action">Upload File Action</a> </body> </html> |
2-fileupload.jsp ADS_TO_REPLACE_3
<%@taglib uri="/struts-tags" prefix="s" %> <html> <head><title>Struts2.2.1_file_Tag_Example</title></head>ADS_TO_REPLACE_4 <body> <h1>Struts2.2.1_file_Tag_Example</h1><hr/> <s:form action="fileUploadAction.action" method="post" ADS_TO_REPLACE_5 enctype="multipart/form-data" namespace="/"> <s:file name="Uploadfile" label="Upload file : "> </s:file> <s:submit label="Submit"></s:submit></s:form>ADS_TO_REPLACE_6 </body> </html> |
3-FileUploadAction.java
package roseindia.action;ADS_TO_REPLACE_7 import java.io.File; import com.opensymphony.xwork2.ActionSupport; public class FileUploadAction extends ActionSupport {ADS_TO_REPLACE_8 private File Uploadfile; private String UploadfileFileName; private String UploadfileContentType;ADS_TO_REPLACE_9 public File getUploadfile() { return Uploadfile; }ADS_TO_REPLACE_10 public void setUploadfile(File uploadfile) { Uploadfile = uploadfile; }ADS_TO_REPLACE_11 public String getUploadfileFileName() { return UploadfileFileName; }ADS_TO_REPLACE_12 public void setUploadfileFileName(String uploadfileFileName) { UploadfileFileName = uploadfileFileName; }ADS_TO_REPLACE_13 public String getUploadfileContentType() { return UploadfileContentType; }ADS_TO_REPLACE_14 public void setUploadfileContentType(String uploadfileContentType) { UploadfileContentType = uploadfileContentType; }ADS_TO_REPLACE_15 public String execute() throws Exception { return SUCCESS; }ADS_TO_REPLACE_16 public String uploadForm() { return NONE;} } |
4_struts.xml
<struts>ADS_TO_REPLACE_17 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false"/> <constant name="struts.custom.i18n.resources" value="struts-messages" />ADS_TO_REPLACE_18 <package name="roseindia" extends="struts-default" namespace="/"> <action name="uploadFileAction" class="roseindia.action.FileUploadAction" method="uploadForm"> <result name="none">jsp/fileupload.jsp</result>ADS_TO_REPLACE_19 </action> <action name="fileUploadAction" class="roseindia.action.FileUploadAction"> <interceptor-ref name="i18n"/> ADS_TO_REPLACE_20 <interceptor-ref name="fileUpload"> <param name="allowedTypes">text/html</param> <param name="maximumSize">200</param>ADS_TO_REPLACE_21 </interceptor-ref> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param>ADS_TO_REPLACE_22 </interceptor-ref> <interceptor-ref name="validation" > </interceptor-ref>ADS_TO_REPLACE_23 <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref>ADS_TO_REPLACE_24 <result name="success">jsp/fileUploadSuccess.jsp</result> <result name="input">jsp/fileupload.jsp</result> </action>ADS_TO_REPLACE_25 </package> </struts> |
5_fileUploadSuccess.jsp
<%@taglib uri="/struts-tags" prefix="s" %> <html>ADS_TO_REPLACE_26 <head><title>Struts2.2.1_file_Tag_Example</title></head> <body > <h1>Struts2.2.1_file_Tag_Example</h1><hr/><h4>ADS_TO_REPLACE_27 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"/>ADS_TO_REPLACE_28 </body> </html> |
6_struts-messages.properties
struts.messages.error.uploading - File uploading failed struts.messages.error.file.too.large - Given file is too largeADS_TO_REPLACE_29 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
ADS_TO_REPLACE_30
fileUploadForm.gif
SelectRightFormatFile.gifADS_TO_REPLACE_31
UploadedFileInfo.gif
ADS_TO_REPLACE_32
Advertisements
Ads
Ads