嘻嘻仔 2016-10-22 05:49 采纳率: 0%
浏览 1340

Struts2上传文件空指针异常

刚刚接触Struts2,原先按着书上打是可以上传文件的,但是莫名奇妙又不行了,
还报空指针异常。
这是显示的错误
图片说明

以下Action里的代码:

public class UploadAction extends ActionSupport {
    private String title; //封装上传文件标题的成员变量
    private File upload; //封装上传文件域的成员变量
    private String uploadContentType; / /封装上传文件类型的成员变量
    private String uploadFileName;// 封装上传文件名字的属性
    private String savePath;
    // 接受struts.xml 文件配值的方法
    public void setSavePath(String value) {
        this.savePath = value;
    }

    // 获取上传文件的保存位置
    private String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public String getTitle() {
        return title;
    }/

    public void setTitle(String title) {
        this.title = title;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    @Override
    public String execute() throws Exception {

        FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
        FileInputStream fis=new FileInputStream(getUpload());
        byte[] buffer= new byte[1024];
        int len=0;
        while((len=fis.read(buffer))>0){
            fos.write(buffer, 0, len);
        }
        return SUCCESS;
    }
}

以下为strtus.xml的代码

 <struts>
   <package name="lee" extends="struts-default" >
     <action name="upload" class="action.UploadAction">
       <param name="savePath">/uploadFiles</param>
      <!-- <param name="allowTypes">image/png,image/bmp,image/gif,image/jpeg</param> -->
       <result> /success.jsp</result>
       <!--  <result name="input">/upload.jsp</result>-->
     </action>

   </package>

在线等!!!!!!谢谢了!

  • 写回答

3条回答 默认 最新

  • 我叫冬瓜 2016-10-22 08:26
    关注

    出现空指针的情况无非就那么几种情况,。。。打个断点看看吧。。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog