String filePath =request.getSession().getServletContext().getRealPath("/");
提示这行代码报空指针。
总代码是这样的:
//得到文件夹路径
String filePath1 = request.getSession().getServletContext().getRealPath("/");
String filePath = filePath1+"prCodeImages";
System.out.println(filePath);
File prCodeFile = new File(filePath);
if(!prCodeFile .exists() && !prCodeFile .isDirectory()){
prCodeFile .mkdir();
}
按道理说当文件不存在,我判断了并新建,所以应该不可能出错。
这里报空指针的原因其实和获取文件没什么关系了。
是getsession时报错,当没有session对象时getsession是会报错的。
String filePath1 = request.getSession(true).getServletContext().getRealPath("/");
这样加个true就没事了。