错误显示如下:
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>