简体   繁体   English

使用struts2上传zip文件

[英]Uploading zip file with struts2

I have checked across various platform for uploading ZIP file using Struts2. 我已经检查过各种平台,以使用Struts2上传ZIP文件。

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. 我从http://struts.apache.org/docs/file-upload.html获得了一些参考,但不值得,我知道我们可以使用Struts 2上传多个文件,但我想将其作为zip文件夹上传。

Is it possible to upload the files using Struts2? 是否可以使用Struts2上传文件?

jsp page JSP页面

<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 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. 通过使用此代码,我一次只能上传单个文件,但是当我尝试zip文件时,contentType,filename却为null。 ..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. 我有多个文件要上传,因此我对所有文件进行了zip压缩,并希望一次使用struts2加载该zip文件。

As I mentioned above, filename or its contentType comes as Null . 正如我上面提到的, filename或其contentTypeNull 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. 所以我的问题是,我是否需要在struts.xml中的某个位置设置setcontentType ,或者是否有任何示例可供我通过struts2上载zip引用。

In Struts.xml 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM