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

环境: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) 


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

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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值