Hi, I am new to sturts2. I am trying to upload a jpg image using Struts2 but no luck till now. It will be great if someone could help me in moving further.
I am using Struts 2.0.8 and i have commons-fileupload-1.2.jar and commons-io-1.3.2.jar in classpath I put debug statement in setter methods. I can see even setter methods are not getting invoked. File, FileName, ContentType are all null at Action class. My code looks like below. MyPhoto.jsp <s:form method="post" action="myPhotoUpload" enctype="multipart/form-data"> <s:file name="myPhoto" cssClass="textbox1" accept="image/*" /> <s:submit name="method:upload" cssClass="btn" value="Upload" /> </s:form> Action class: MyPhotoUploadAction.java private File myPhoto;//The actual file private String myPhotoContentType; //The content type of the file private String myPhotoFileName; //The uploaded file name public File getMyPhoto() { return myPhoto; } public void setMyPhoto(File myPhoto) { this.myPhoto = myPhoto; } public String getMyPhotoContentType() { return myPhotoContentType; } public void setMyPhotoContentType(String myPhotoContentType) { this.myPhotoContentType = myPhotoContentType; } public String getMyPhotoFileName() { return myPhotoFileName; } public void setMyPhotoFileName(String myPhotoFileName) { this.myPhotoFileName = myPhotoFileName; } public String upload() { try{ File destDir = new File("images/member"); FileUtils.copyFileToDirectory (getMyPhoto(), destDir); return SUCCESS; // redirect to welcome page. }catch(Exception exp){ LOG.error("File Upload Error:"+ exp); this.addActionError("File Upload Error:"+ exp); return ERROR; } } Struts.xml <struts> <package name="Non-RPC" namespace="/" extends="tiles-default"> <result-types> <result-type name="tiles" class=" org.apache.struts2.views.tiles.TilesResult" default="true" /> </result-types> <action name="myPhotoUpload_*" method="{1}" class=" com.rawatsoft.write4smile.webapp.action.MyPhotoUploadAction"> <interceptor-ref name="fileUpload"> <param name="allowedTypes"> image/* </param> </interceptor-ref> <interceptor-ref name="basicStack"/> <result name="input">myPhotoUpload</result> </action> </package> web.xml <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </filter-class> </filter> <filter> <filter-name>filterDispatcher</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>filterDispatcher</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <listener> <listener-class>org.apache.struts2.tiles.StrutsTilesListener </listener-class> </listener>