您的位置:首页 > 产品设计 > UI/UE

获取文件路径报空指针

2016-01-06 10:43 375 查看
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就没事了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息