两颗大白菜 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 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM