嘻嘻仔 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 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码