两颗大白菜 2017-05-25 12:32 采纳率: 0%
浏览 978

急,action类上传文件时报空指针错误,错误如下,专家们帮我看看

错误显示如下:
java.lang.NullPointerException
java.io.File.(File.java:360)
com.action.uploadtAction.execute(uploadtAction.java:131)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

出错行:
FileUtils.copyFile(upload, new File(file, uploadFileName));

源码:
public class uploadtAction extends ActionSupport {
private File uploadt;
private String uploadContentType;
private String uploadFileName;
private String result;
private String className;
private int id;
private String title;
private String descri;
private String time;
private String pic;

public String getClassName() {
    return className;
}

public void setClassName(String className) {
    this.className = className;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getDescri() {
    return descri;
}

public void setDescri(String descri) {
    this.descri = descri;
}

public String getTitle() {
    return title;
}

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

public String getTime() {
    return time;
}

public void setTime(String time) {
    this.time = time;
}

public String getPic() {
    return pic;
}

public void setPic(String pic) {
    this.pic = pic;
}

public File getUploadt() {
    return uploadt;
}

public void setUploadt(File upload) {
    this.uploadt = 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;
}

public String getResult() {
    return result;
}

public void setResult(String result) {
    this.result = result;
}

// 实现execute方法
public String execute() throws Exception {
    HttpServletRequest reqeust = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();

    String path = ServletActionContext.getServletContext().getRealPath(
            "/img");
    File file = new File(path);
    if (!file.exists()) {
        file.mkdir();

    }
    FileUtils.copyFile(uploadt, new File(file, uploadFileName));
    result = "上传成功";

    news n = new news();
    newsDao nDao = new newsDao();
    Date date = new Date();

    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    String time = format.format(date);
    String className = (String) reqeust.getParameter("r");
    String title = (String) reqeust.getParameter("title");
    String descri = (String) reqeust.getParameter("descri");
    String pic = "img/" + uploadFileName;; // 获取表单传过来的数据

    n.setClassName(className);
    n.setTitle(title);
    n.setTime(time);
    n.setDescri(descri);
    n.setPic(pic);

    try {
        // 执行数据库查询操作
        nDao.addNews(n);

    } catch (SQLException e) {
        e.printStackTrace();
    }
    // }

    return SUCCESS;

}

struts.xml

uploadNews.jsp
error.jsp

jpg,jpeg,JPG,JPEG
10M

        <interceptor-ref name="defaultStack"></interceptor-ref>

    </action>

  • 写回答

2条回答 默认 最新

  • 两颗大白菜 2017-05-25 12:28
    关注

    struts.xml如下:

    uploadNews.jsp
    error.jsp

    jpg,jpeg,JPG,JPEG
    10M

            <interceptor-ref name="defaultStack"></interceptor-ref>
    
        </action>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图