【问题标题】:Uploading zip file with struts2使用 struts2 上传 zip 文件
【发布时间】:2016-06-14 09:17:30
【问题描述】:

我已经检查了使用 Struts2 上传 ZIP 文件的各种平台。

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

是否可以使用 Struts2 上传文件?

jsp页面

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

动作类

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>

通过使用此代码,我一次只能上传单个文件,但是当我尝试使用 zip 文件时,我的 contentType 文件名为 null。 ..添加了我现在尝试但仍然没有运气的更新代码。

编辑

对不起,如果我的问题对您来说不清楚...添加更多点和更新的代码,我刚刚尝试过但无法正常工作。

我有多个文件要上传,所以我对所有文件进行了 zip 压缩,并希望使用 struts2 一次性加载该 zip 文件。

正如我上面提到的,filename 或其contentTypeNull。 所以我的问题是,我需要 setcontentType 在 struts.xml 中的某个地方吗,或者如果您有任何示例,我可以参考通过 struts2 上传 zip。

【问题讨论】:

  • 什么是 zip 文件夹?
  • 如果不是默认命名空间,也要在jsp中写入命名空间。
  • 发布您遇到的错误。
  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅“如何提问”页面以获得澄清此问题的帮助。

标签: java jsp struts2


【解决方案1】:

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>

【讨论】:

    猜你喜欢
    • 2012-01-09
    • 2015-02-02
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 2012-10-16
    • 1970-01-01
    相关资源
    最近更新 更多