gpt4 book ai didi

java - 上传多个文件时,Struts 2 操作中的文件对象数组为空/空

转载 作者:太空宇宙 更新时间:2023-11-04 14:35:55 26 4
gpt4 key购买 nike

我尝试在 Struts 2 应用程序中上传多个文件,但每次我收到 File[] fileUpload is empty 。我在 struts.xml 中进行了一些配置更改,但仍然将 fileUplaod 对象设置为 null 或 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,一旦成功打印文件详细信息,就会呈现该文件。

最佳答案

如果要给fileUpload interceptor添加参数您应该通过引用 defaultStack 并使用拦截器的名称作为参数名称的前缀或重建默认堆栈来完成此操作,但不应在操作中重复 fileUpload 拦截器配置。例如

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

关于java - 上传多个文件时,Struts 2 操作中的文件对象数组为空/空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25581842/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com