Struts 2中的NullPointException上传文件

时间:2017-05-23 20:37:30

标签: file-upload configuration struts2 struts2-interceptors interceptorstack

我正在尝试使用表单上传文件,我正在按照互联网上的教程进行操作。使用此代码,我收到#reshape the array to 2 columns, then sum columns, finally reshape it back to 2 columns. a.reshape(-1,2).sum(1).reshape(-1,2) Out[102]: array([[ 1, 5], [ 9, 13], [17, 21]])

这是index.jsp:

NullPointerException

这是success.jsp(如果上传成功):

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>File Upload</title>
    </head>
    <body>
    <form action="upload" method="post" enctype="multipart/form-data">
     <label for="myFile">Upload your file</label>
    <input type="file" name="myFile" />
    <input type="submit" value="Upload"/>
   </form>
    </body>

Action类:

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>File Upload Success</title>
</head>
<body>
  You have successfully uploaded <s:property value="myFileFileName"/>
</body>
</html>

Struts.xmf配置文件:

    import java.io.File;
    import org.apache.commons.io.FileUtils;
    import java.io.IOException; 

    import com.opensymphony.xwork2.ActionSupport;

    public class uploadFile extends ActionSupport{
    private File myFile;
    private String myFileContentType;
    private String myFileFileName;
    private String destPath;

      public String execute()
   {
                    /* Copy file to a safe location */
               destPath = "C:/apache-tomcat-6.0.33/work/";

      try{
         System.out.println("Src File name: " + myFile);
         System.out.println("Dst File name: " + myFileFileName);

         File destFile  = new File(destPath, myFileFileName);
         FileUtils.copyFile(myFile, destFile);

      }catch(IOException e){
         e.printStackTrace();
         return ERROR;
      }

      return SUCCESS;
   }
        public File getMyFile() {
        return myFile;
    } 
        public void setMyFile(File myFile) {
         this.myFile = myFile;
   }
   public String getMyFileContentType() {
      return myFileContentType;
   }
   public void setMyFileContentType(String myFileContentType) {
      this.myFileContentType = myFileContentType;
   }
   public String getMyFileFileName() {
      return myFileFileName;
   }
   public void setMyFileFileName(String myFileFileName) {
      this.myFileFileName = myFileFileName;
   }
}

注意: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.multipart.maxSize" value="1000000" /> <package name="helloworld" extends="struts-default"> <action name="upload" class="com.tutorialspoint.struts2.uploadFile"> <interceptor-ref name="basicStack"> </interceptor-ref> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/jpeg,image/gif</param> </interceptor-ref> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts> 相当于error.jsp,只会在正文中打印不同的消息。

1 个答案:

答案 0 :(得分:0)

Toolbar拦截器必须在FileUpload拦截器堆栈之前运行。

更改此

BasicStack

到这个

<action name="upload" class="com.tutorialspoint.struts2.uploadFile">
   <interceptor-ref name="basicStack" />
   <interceptor-ref name="fileUpload">
       <param name="allowedTypes">image/jpeg,image/gif</param>
    </interceptor-ref>