简体   繁体   English

在struts2中上传文件

[英]File upload in struts2

I'm trying to upload a file using struts2. 我正在尝试使用struts2上传文件。

In the jsp page there is a button for uploading the file and a Submit button. 在jsp页面中有一个用于上传文件的按钮和一个Submit按钮。 No error message is shown in the program, but when I click the Submit button nothing happens. 程序中没有显示错误消息,但是当我单击“ Submit按钮时没有任何反应。 See my code below: 请参阅下面的代码:

Action class 动作类

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 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 config 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>

You have a self closing form... 你有一个自我结束的形式......

Try like this: 试试这样:

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

Thanks to Anu for corrections, +1... 感谢Anu进行更正,+1 ......

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

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