帅气的张张
2018-03-11 08:50
采纳率: 66.7%
浏览 1.5k
已结题

struts2 上传文件 uploadAction中的File名称NULL,实在不知道哪里错了!!

struts.xml文件

 <struts>
<!-- 设置文件上传允许最大值 为10M-->
<constant name="struts.multipart.maxSize" value="10485760"></constant>
<!-- 配置上传文件的出错信息的资源文件 -->
<constant name="struts.custom.i18n.resources" value="cn.itcast.action.FileUploadMessage"/>
<constant name="struts.multipart.saveDir" value="/upload"/>
<package name="struts2" namespace="/" extends="struts-default">

<!-- 简单文件上传 -->
<!--  <action name="fileUpload" class="cn.itcast.action.FileUploadAction"> -->
<!--        <result name="success">/result.jsp</result> -->
<!--  </action> -->

<!-- 文件上传(限制大小和类型) -->
 <action name="fileUpload" class="gzy.FileUploadAction">
    <result name="success">/index.jsp</result>
    <!--定义上传出错要跳转的页面 -->
    <result name="input">/login.jsp</result>
    <interceptor-ref name="defaultStack">

    </interceptor-ref>
 </action>


</package>
</struts>

login.jsp文件

 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login</title>
</head>
<body>
<s:form action="fileUpload" method="post" entype="multipart/form-data">
<s:textfield name="test" label="测试文本"></s:textfield>
<s:file label="上传文件" name="doc"></s:file>
<s:submit value="上传"/>
</s:form>
<br/>
<br/>

</body>
</html>

fileuploadaciton.java

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport{
    private static final long serialVersionUID=1L;
    //提交过来的文件
    private File doc;
    //提交过来的file的名字
    private String docFileName;
    //提交过来的file的类型
    private String docContentType;
    private String test;
    public String getTest(){
        return test;
    }
    public void setTest(String test){
        this.test=test;
    }
    public File getFile()
    {
        return doc;
    }
    public void setFile(File file)
    {
        this.doc = file;
    }
    public String getFileFileName()
    {
        return docFileName;
    }
    public void setFileFileName(String fileFileName)
    {
        this.docFileName = fileFileName;
    }
    public String getFileContentType()
    {
        return docContentType;
    }
    public void setFileContentType(String fileContentType)
    {
        this.docContentType = fileContentType;
    }
    public String execute() throws Exception
    {
     //文件输入流

     InputStream is = new FileInputStream(doc);
     //设置文件保存的目录
        String uploadPath = ServletActionContext.getServletContext()
                            .getRealPath("/upload");
        //设置目标文件
        File toFile = new File(uploadPath, this.getFileFileName());
        //文件输出流
        OutputStream os = new FileOutputStream(toFile);
        byte[] buffer = new byte[1024];
        int length = 0;
        //读取file文件输出到toFile文件中
        while(-1 != (length = is.read(buffer, 0, buffer.length)))
        {
            os.write(buffer);
        }
        //关闭输入流和输出流
        is.close();
        os.close();
        return SUCCESS;
    }
}

总是提示我的aciton类错了java.lang.NullPointerException
java.io.FileInputStream.(Unknown Source)
gzy.FileUploadAction.execute(FileUploadAction.java:56)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

就是这一行:
     //文件输入流

     InputStream is = new FileInputStream(doc);

求大神

  • 点赞
  • 收藏

3条回答 默认 最新

  • 龙图腾之祥龙 2018-03-11 08:54
    已采纳


    应该是:
    enctype

    点赞 评论
  • Luxd0412 2018-03-11 08:58

    就是你调用的这个函数却没有引入相关的类

    点赞 评论
  • weixin_40826791 2018-03-13 11:12

    public File getFile()
    {
    return doc;
    }
    public void setFile(File file)
    {
    this.doc = file;
    }

        改成setDoc和getDoc
    
    点赞 评论

相关推荐 更多相似问题