9

I need to upload "1000 files" or "a zip file including all the files" at once using Struts2. (By 1000 Files or a zip file, I mean I need all the files to be uploaded on the system it does not matter if user choose all the 1000 files at once or zip them and upload as a single file, so I am looking for the one which is easier to implement and more efficient)

I have read the following answers but none of them suits the purpose.

Using the following code, when I use a simple List files; it shows name of lists, but when I use List files it does not show any thing and I can not upload the files.

upload.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="upload" enctype="multipart/form-data" method="post">
            <input name="files" type="file" multiple/>
            <button type="submit"/>
        </form>
    </body>
</html>

upload.java

@Action
public class upload implements Addresses {

    private List <File> files = new ArrayList <File> ();

    public String execute(){

        return "success";
    }
    public upload() {
        System.out.println("in upload 1");

         for(int i=0;i<files.size();i++)
            System.out.println(i + ")" + files.get(i));

        System.out.println("in upload 2");
    }

    public List <File> getFiles() {
        return files;
    }

    public void setFiles(List <File> files) {
        this.files = files;
        for(int i=0;i<files.size();i++)
            System.out.println(i + ")" + files.get(i));
//            File fileToCreate = new File("c:\image", files.get(i).toString());
//            FileUtils.copyFile(files.get(i), fileToCreate);
    }
}

Output

in upload 1
in upload 2
Community
  • 1
  • 1
J888
  • 1,804
  • 8
  • 39
  • 73
  • 2
    it's quite old to use ` – Raptor May 06 '13 at 07:06
  • Twisting your question: can you zip the 1000 files in 1 ZIP file and upload it, then extract in server side and obtain the file list ? – Raptor Jun 20 '13 at 02:52
  • @ShivanRaptor Yes, question is updated – J888 Jun 20 '13 at 03:02
  • @Jack... if you can upload a zip with 1000 files, then it is a simple one file upload... ?!! In what your question differs from a single file upload ? – Andrea Ligios Jun 20 '13 at 09:02
  • @JackRamzi You should be concrete what do you want 1000 files upload or 1 zip file. The last option should be defined how the zip should be created. – Roman C Jun 20 '13 at 19:07
  • @JackRamzi answer is here http://stackoverflow.com/a/31563029/3649347 – Tell Me How Jul 22 '15 at 12:27

2 Answers2

9

I suggest you to use Struts Tags instead of plain HTML tags, and to extend ActionSupport (returning its Result constants instead of manually composing the result String, like "result").

That said, this is a tested and working example.

Note: It won't work on old versions of IE, but since you are using HTML5 in your own question, I bet you already know it and you are not targeting old IE.


JSP

<%@page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Multiple File Upload Example</title>
    </head>
    <body>
        <s:form action="upload" enctype="multipart/form-data" >
            <s:file name="files" multiple="multiple" />
            <s:submit value="Upload files" />
        </s:form>
    </body>
</html>

Note about the multiple="multiple" part: even if in the official documentation, that attribute for the <s:file /> tag is not defined, since Struts 2.1 it is allowed because of

Dynamic Attributes Allowed: true

this means that it will be drawn on the JSP as-is, without any interference by Struts. This way Struts doens't need to update its Tags each time HTML5 provides a new feature; you could put foo="bar" too in a tag that allows Dynamic Attributes (<s:file />, <s:textarea />, etc), and you will find it in the HTML.

Action

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

}

Then you may want to set the maximum size of the Request, and the maximum size of each single file, like described here:

Struts.xml - Max multipart size:

<constant name="struts.multipart.maxSize" value="20000000" /> 

Struts.xml - Max size of a file (globally to a package, or locally to an Action)

<interceptor-ref name="fileUpload">
    <param name="maximumSize">10485760</param>
</interceptor-ref>
Community
  • 1
  • 1
Andrea Ligios
  • 46,329
  • 24
  • 102
  • 208
  • Hi , I tried exact code and it is not working on struts 2.5.13. File name is always null . Can you help me ? – aatif Feb 17 '18 at 16:21
  • Check your Interceptor Stack. Carefully :) – Andrea Ligios Feb 17 '18 at 16:53
  • Especially https://stackoverflow.com/a/29211248/1654265 and https://stackoverflow.com/a/33697612/1654265 – Andrea Ligios Feb 17 '18 at 16:58
  • i copied exact same thing given above. It works for single file upload but not for multiple. Started since I have upgraded to struts 2.5.13. – aatif Feb 19 '18 at 02:34
  • The code above does *not* include the Interceptor Stack, hence my comment. Also, in which version your code was working, if any? Have you checked if the upload is raising an exception (which causes Common FileUpload library, used by FileUploadInterceptor, to drop params) ? Finally, can you generate a Struts 2.5.13 project from a maven archetype and try a simple multiple upload on a clean project to see if it works? It should take no more than 15 minutes. Feel free to start a new question linking it here, or asking on Apache JIRA, but they'll need an SSCCE too. – Andrea Ligios Feb 19 '18 at 08:37
  • 1
    Thank you for your response Andrea. Have created new project for 2.5.13. There was issue with some jar, possibly javaassit. Now it is working after adding new jars. Migration guy didn't add jars properly. Thanks once again – aatif Feb 22 '18 at 03:41
  • Glad that helped. Sometimes restarting from scratch can save you a lot of headaches :) – Andrea Ligios Feb 22 '18 at 08:37
0

In JSP file:-

<form action="doUpload" enctype="multipart/form-data" method="post">
      <s:file name="fileUpload" multiple="multiple" label="Pick files" size="30"/>
      <br/>
      <s:submit value="Upload All" />
</form>

In .java file

 private File fileUpload[];
 private String fileUploadFileName[];
 private String fileUploadContentType[];
 private String saveDirectory;

/** setter and getter for all above variables**/


public String doUpload1()throws Exception{
            for (int i=0; i<fileUpload.length;i++){
                File uploadedFile = fileUpload[i];
                String fileName = fileUploadFileName[i];
                File destFile = new File(saveDirectory + File.separator + fileName);
                try {
                    FileUtils.copyFile(uploadedFile, destFile);
                } catch (IOException ex) {
                    System.out.println("Could not copy file " + fileName);
                    ex.printStackTrace();
                }
            }
        return "success";
    }

This is one is working for me , hope will work for you, please let me know if any issue.

Rahul Kumar
  • 145
  • 9