Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
689 views
in Technique[技术] by (71.8m points)

jsp - Struts2 File Upload

I want to upload multiple files in one form and that number of files is varying from user to another, ex. a user may upload 2 files and another user could upload 12 files. I want to know how could I create a loop or something in the jsp struts2 form such that when a user uploads a file it adds its name to a list and he/she could upload another file and be added to the list and so on.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use build in Struts2 multiple file uploading feature.In your Jsp file you have to define something like

<s:form action="doMultipleUploadUsingList" method="POST" enctype="multipart/form-data">
    <s:file label="File (1)" name="upload" />
    <s:file label="File (2)" name="upload" />
    <s:file label="FIle (3)" name="upload" />
    <s:submit />
</s:form>

which will send these files to Your action class and you have the option of Collecting the file content as a list

public class MultipleFileUploadUsingListAction extends ActionSupport {

    private List<File> uploads = new ArrayList<File>();
    private List<String> uploadFileNames = new ArrayList<String>();
    private List<String> uploadContentTypes = new ArrayList<String>();
    // There getter and setter methods

}

here

[File Name] : File - the actual File
[File Name]ContentType : String - the content type of the file
[File Name]FileName : String - the actual name of the file uploaded (not the HTML name)

For detail refer to the multiple file upload page of Struts2

Multiple File Upload

Showing user an option to add more file on the JSP page is matter of how to want to display that.All you need to take care when you show him file-upload box name should be same so that Struts2 param interceptor can set them as a list in your Action class


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...