我正在尝试在 Struts 2 应用程序中上传多个文件,但每次我得到File[] fileUpload
的是空的。我已经进行了一些配置更改,struts.xml
但仍然将fileUplaod
object 设置为 anynull
或 empty 。有人能告诉我我该怎么做才能让它工作吗
相应的操作代码如下:在此操作中,我正在检索文件对象数组并打印详细信息
编辑 :
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;
}
}
struts.xml
是:我能够使用相同的配置集获取单个文件上传的文件对象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>
然后success.jsp
在成功打印文件详细信息后呈现。