Multiple file uploads in struts 2

2019-02-28 11:31发布

问题:

I have a problem in uploading files using struts2. I have multiple file tags like

<s:file name="fileUpload_5534" multiple="multiple"/>

<s:file name="fileUpload_5585" multiple="multiple"/>

<s:file name="fileUpload_5595" multiple="multiple"/>

These file tags are created dynamically and again can have multiple files uploads as I have specified multiple="multiple". Can anyone suggest the solution for this kind of uploads.

回答1:

You can upload multiple files from a single <s:file> element with multiple="multiple" like described here.

You can also upload multiple files from many <s:file> elements (that allow a single file for each one) in the same way, handling the names of the <s:file>s to point to a list on the Action.

Do you really want to upload a Lists of Lists of Files ?

If yes, I suggest you to model an object, like MyFileListObject, containing the lists of data needed:

class MyFileListObject {
    private List<File> files;
    private List<String> filesContentType;
    private List<String> filesFileName;    

    /* getters and setters */
}

and then expose a List<MyFileListObject> through the Action.

Alternatively, you can granulate it more, defining a new object, like MyFileObject,

class MyFileObject {
    private File files;
    private String filesContentType;
    private String filesFileName;    

    /* getters and setters */
}

,listed in MyFileListObject:

class MyFileListObject {
    private List<MyFileObject> files;

    /* getter and setter */
}

and then expose a List<MyFileListObject> through the Action.

But it seems overkill to me... which kind of page should let many <input type="file"/> upload many files each one in a single post ?