0
votes

I have checked across various platform for uploading ZIP file using Struts2.

I get some reference from http://struts.apache.org/docs/file-upload.html but not worth I know we can upload the multiple files using Struts 2 but I would like to upload it as zip folder.

Is it possible to upload the files using Struts2?

jsp page

<s:form action="doUpload" method="post" enctype="multipart/form-data">
    <s:file name="upload" label="File"/>
    <s:submit/>
</s:form>

action class

public class UploadAction extends ActionSupport {
  private File[] upload;
  private String contentType;
  private String[] filename;


public File[] getFileUploads() {
    return upload;
}

public void setFileUploads(File[] fileUploads) {
    this.upload= fileUploads;
}

 public String[] getUploadsFileNames()
  {
    return filename;
  }

  public void setUploadsFileName(String[] uploadFileNames)
  {
    this.filename= uploadFileNames;
  }

  public String execute() {
try{
    String localPath = "C:\\tmp\\localDir";
    for (int i = 0; i < upload.length; i++)
     {
            File fileToCreate = new File(downloadDir.getAbsolutePath(), voiceBasePromptsFileNames[i]);
            FileUtils.copyFile(upload[i], fileToCreate);
     }
}catch(Exception e){}
       return SUCCESS;
  }
}

sturts.xml

<action name="doUpload" class="com.example.UploadAction">
            <param name="contentType">application/zip</param>
            <param name="inputName">zipFileInputStream</param>
            <param name="contentDisposition">attachment;filename="${fileName}"</param>
            <param name="bufferSize">1024</param>
            <param name="contentLength">${contentLength}</param>
        <result type="json" />
        <interceptor-ref name="defaultStack"></interceptor-ref>
</action>

by using this code I can only upload the single file at a time but when I tried for zip file I got null for contentType , filename. ..Added updated code which I tried now but still didnt get any luck.

EDIT

Sorry if my question is not clear to you...adding some more point and updated code which I just tried but not working.

I have multiple file to upload, so I make a zip of all files and want to load that zip file using struts2 in one shot.

As I mentioned above, filename or its contentType comes as Null. So my question is, do I need to setcontentType somewhere in struts.xml or if you have any example which I can refer for uploading zip through struts2.

1
What is a zip folder ? - Andrea Ligios
write namespace also in jsp if it is not default. - prem30488
Post error you are getting. - prem30488
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. - Roman C

1 Answers

0
votes

In Struts.xml

 <action name="resultAction" class="com.mkyong.common.action.FileUploadAction">
    <interceptor-ref name="exception"/>
        <interceptor-ref name="i18n"/>
        <interceptor-ref name="fileUpload">
        <param name="allowedTypes">text/plain,application/zip</param>
        <param name="maximumSize">10240</param>
    </interceptor-ref> 
        <interceptor-ref name="params">
            <param name="excludeParams">dojo\..*,^struts\..*</param>
        </interceptor-ref>
        <interceptor-ref name="validation">
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
        <interceptor-ref name="workflow">
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>

    <result name="success">pages/result.jsp</result>
    <result name="input">pages/fileupload.jsp</result>

</action>