gpt4 book ai didi

java - 在Struts2中上传多个文件并维护索引(文件上传的位置)

转载 作者:行者123 更新时间:2023-11-30 02:44:34 25 4
gpt4 key购买 nike

我有一个场景,我想上传多个文件,其中用户可能会或可能不会上传文件,并且我想维护用户上传文件的位置的索引,并希望使用该索引作为名称保存文件

我提到了https://stackoverflow.com/a/17050230/3425489 ,就我而言,我不想创建新类,因此未提及接受的解决方案

到目前为止,在我的 Action 课中,我已经

File upload [];
String uploadContentType []
String uploadFileName []

getter 和 setter

在我的jsp中我尝试过

<input type="file" name="upload">

但我只能获取上传的文件,无法维护索引

也尝试过

<input type="file" name="upload[0]">
<input type="file" name="upload[1]">
<input type="file" name="upload[2]">

在这种情况下,我无法在我的 Action 类中设置Properties

----已更新----

您可以引用我的型号 Struts 2 : Unable to access Model properties in JSP

对于您的每个 ProcessSolutionStep,我想维护为特定步骤上传的文件,

即用户可以上传第 1 步第 5 步的文件,跳过中间步骤并查看。

我想显示特定步骤上传的文件

最佳答案

不需要创建一个新类(这是一种方法,如果您喜欢单独封装每个对象),只需使用 Lists:

public class Upload extends ActionSupport{

private List<File> files;
private List<String> filesContentType;
private List<String> filesFileName;

/* GETTERS AND SETTERS */

public String execute() throws Exception{
System.out.print("\n\n---------------------------------------");
int i=0;
for (File file : files){
System.out.print("\nFile ["+i+"] ");
System.out.print("; name:" + filesFileName.get(i));
System.out.print("; contentType: " + filesContentType.get(i));
System.out.print("; length: " + file.length());
i++;
}
System.out.println("\n---------------------------------------\n");
return SUCCESS;
}

}

使用multiple属性,并且不要忘记正确的enctype:

<s:form action="upload" enctype="multipart/form-data" >
<s:file name="files" multiple="multiple" />
<s:submit value="Upload files" />
</s:form>

关于java - 在Struts2中上传多个文件并维护索引(文件上传的位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40591256/

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