I am trying to upload multiple files in Struts 2 application but every time I am getting File[] fileUpload
is empty . I have made several configuration changes in struts.xml
but still getting fileUplaod
object as either null
or empty . Can someone tells me what am I supposed to do to get it working
The corresponding action code is as : In this action I am retrieving the file object array and printing the details
EDIT :
DummyFileUploadAction.java
:
package com.cbuddy.common.action;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class DummyFileUploadAction extends ActionSupport{
private File[] fileUpload;
private String fileUploadFileName;
private String[] fileUploadContentType;
public File[] getFileUpload() {
return fileUpload;
}
public void setFileUpload(File[] fileUpload) {
this.fileUpload = fileUpload;
}
public String getFileUploadFileName() {
return fileUploadFileName;
}
public void setFileUploadFileName(String fileUploadFileName) {
this.fileUploadFileName = fileUploadFileName;
}
public String[] getFileUploadContentType() {
return fileUploadContentType;
}
public void setFileUploadContentType(String[] fileUploadContentType) {
this.fileUploadContentType = fileUploadContentType;
}
@Override
public void validate() {
if (null == fileUpload) {
System.out.println("DummyFileUploadAction.validate()");
}
}
public String uplaod(){
return "success";
}
public String execute() throws Exception{
for (File file: fileUpload) {
System.out.println("File :" + file);
}
for (String fileContentType: fileUploadContentType) {
System.out.println("File type : " + fileContentType);
}
return SUCCESS;
}
}
The struts.xml
is : I was able to get file object for single file upload with same set of configuration in struts.xml
struts.xml
:
<!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="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.multipart.maxSize" value="1000000" />
<package name="default" extends="struts-default,json-default" namespace="/">
<action name="upload" class="com.cbuddy.common.action.DummyFileUploadAction" method="uplaod">
<result name="success">/uplaod.jsp</result>
</action>
<action name="dummyUpload" class="com.cbuddy.common.action.DummyFileUploadAction" >
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/gif,image/png</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/uplaod.jsp</result>
</action>
</package>
</struts>
And then success.jsp
which will be rendered once files details are successfully printed .
If you want to add parameters to fileUpload
interceptor you should do it by referencing a defaultStack
and use name of the interceptor to prefix a param name or reconstruct the default stack, but you should not duplicate the fileUpload
interceptor in the action config. For example
<action name="dummyUpload" class="com.cbuddy.common.action.DummyFileUploadAction" >
<interceptor-ref name="defaultStack">
<param name="fileUpload.allowedTypes">image/jpeg,image/gif,image/png</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/uplaod.jsp</result>
</action>