我正在尝试使用struts2上传文件。

在jsp页面中,有一个用于上传文件的按钮和一个Submit按钮。程序中未显示任何错误消息,但是当我单击Submit按钮时,没有任何反应。请参阅下面的代码:

动作类

package com.scrolls.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.util.Map;
import org.apache.commons.io.FileUtils;

public class UploadAction extends ActionSupport {

  private File upload;
  private String uploadContentType;
  private String uploadFileName;

  public String fileUpload() {
      try {
          String fullFileName = "c:/sample/mystruts/myfile.txt";
          File theFile = new File(fullFileName);
          FileUtils.copyFile(upload, theFile);
      } catch (Exception e) {
          System.out.println(e.toString());
          return ERROR;
      }

      return SUCCESS;
  }

  // Plus public getters/setters for upload properties.
}

JSP

<%@taglib uri="/struts-tags" prefix="s" %>
<html>
    <head>
        <s:head theme="ajax" />
    </head>

    <body>
        <s:form action="doUpload.action" enctype="multipart/form-data"/>
        <s:datetimepicker name="date" displayFormat="yyyy-MM-dd" />
        <s:file name="upload" />
        <s:submit value="submit"/>
    </body>
</html>

Struts配置

<struts>
  <package name="register3" extends="struts-default">
    <action name="doUpload" class="com.scrolls.action.UploadAction" method="fileUpload">
      <result name="success">/suc.jsp</result>
      <result name="error">/fail.jsp</result>
    </action>
  </package>
</struts>

最佳答案

您有一个自我关闭表格...

尝试这样:

<s:form action="doUpload.action"
        method="POST"
        enctype="multipart/form-data" >
    <s:datetimepicker label="Select Date"
                     name="date"
                     displayFormat="yyyy-MM-dd"
                     required="true" />
    <s:file label="File:" name="upload" />
    <s:submit value="submit" />
</s:form>

感谢Anu的更正,+ 1 ...

09-25 17:22