[Exception Shoot]-NullPointerException when uploading a file(struts文件上传)

来源:互联网 发布:淘宝网现状分析 编辑:程序博客网 时间:2024/05/16 15:12
环境:SSH
要实现功能:在工程添加一个文件上传功能,,已经做了中文字符处理、文件大小处理。文件上传时出现了 NullPointerException 。

    1. Action代码:
      private File file;private String contentType;private String fileName;                    public String fileUpload() throws Exception{    File saveFile = new File("/Users/Zus/Desktop/securitymgr/WebRoot/exportFiles/" + fileName);    String status =null;    if (!saveFile.getParentFile().exists()){        saveFile.getParentFile().mkdirs();        status="成功的边缘";    }    FileUtils.copyFile(file, saveFile);// 复制文件    this.addFieldError("isSuccess", "文件上传成功!");    servletHandler.writeToClient(ServletActionContext.getResponse(), status);    return SUCCESS;}


     2.struts-file.xml :
 <constant name="struts.i18n.encoding" value="GBK" />  <constant name="struts.multipart.maxSize" value="3145728" /><constant name="struts.multipart.saveDir" value="/Users/Zus/Desktop/securitymgr/WebRoot/exportFiles" /><package name="test" extends="json-default" namespace="/test">    <default-interceptor-ref name="defaultStack"/>    <action name="test_*" class="action.test.TestAction" method="{1}">        <result type="json" />    </action>    <!--<action name="test*" class="action.test.TestAction" method="{1}">-->        <!--<result type="file"></result>-->    <!--</action>--></package>



     3.     jsp页面:
                    
<span style="white-space:pre"></span><pre name="code" class="html">  <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>  <title>文件上传</title>  <meta http-equiv="pragma" content="no-cache">  <meta http-equiv="cache-control" content="no-cache">  <meta http-equiv="expires" content="0"></head><body><form action="${pageContext.request.contextPath}/test/test_fileUpload.action"      enctype="multipart/form-data" method="post">  文件:<input type="file" name="file">  <input type="submit" value="上传" /></form><br/><s:fielderror /></body>
<span style="font-family: 'Helvetica Neue'; font-size: 12px;"></html></span>



    4.上传文件的时候出现了 :
Struts Problem Report Struts has detected an unhandled exception: Messages: File: java/io/File.java Line number: 317 
Stacktraces java.lang.NullPointerException java.io.File.(File.java:317) 
    example.uploadFile.execute(uploadFile.java:36) 

StackOverFlow:可能是拦截器问题: http://stackoverflow.com/questions/29210254/nullpointerexception-when-uploading-a-file  

修改后还是出错,发现不是这个问题》》》

              看了以前的工程后发现命名有规则,尝试了一下竟然通过了;
               jsp中如果<input type=“file” name=“anyThing”> 
               那么在Action中必须为:
               private StringanyThingContentType;
                       private StringanyThingFileName;


到现在还是不知道这是什么原因;望各位在评论区留下你的观点或者m我   **whoszus#yahoo.com**
0 0
原创粉丝点击