我有一个方案,我想上传多个文件,用户可以上传也可以不上传文件,并且我想维护用户已上传文件的位置的索引,并希望使用该索引将文件保存为名称

我引用了https://stackoverflow.com/a/17050230/3425489 ,就我而言,我不想创建新的类,因此不推荐接受的解决方案

到现在为止,在我的动作班中

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


吸气剂和二传手

在我的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类中设置setProperties

- - 更新 - -

您可以参考我的模型Struts 2 : Unable to access Model properties in JSP

我要为您的每个ProcessSolutionStep维护,为特定步骤上传哪个文件,

即,用户可以上载步骤1和步骤5的文件,跳过中间步骤并显示在视图中。

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

最佳答案

无需创建新类(这是一种方法,如果您更愿意将每个对象封装在一起),只需使用List s:

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/

10-14 16:45
查看更多