这是Java类中的代码。
图片文件夹中存在redflaglight.png图片
找不到文件Null指针异常位于
inputStream =新的FileInputStream(path);
Struts2.3.5 Java7
public String createRootPath() throws UnsupportedEncodingException {
String rootPath = "";
String path = ExportExcelBusiness.class.getProtectionDomain()
.getCodeSource().getLocation().getPath();
File f = new File(path);
String ff = f.getParent();
f = new File(ff);
ff = f.getParent();
f = new File(ff);
ff = f.getParent();
f = new File(ff);
ff = f.getParent();
f = new File(ff);
ff = f.getParent();
f = new File(ff);
ff = f.getParent();
f = new File(ff);
ff = f.getParent();
String decodedPath = URLDecoder.decode(ff, "UTF-8");
rootPath = decodedPath.replace('\\', '/');
rootPath += "/WebContent/pages/appsresponse/images";
return rootPath;
}
path = createRootPath()+ "/up_arrow_export.png";
InputStream inputStream = null;
int pictureIdx = 0;
byte[] bytes;
try {
inputStream = new FileInputStream(path);
bytes = IOUtils
.toByteArray(inputStream);
pictureIdx = wb.addPicture(bytes,
Workbook.PICTURE_TYPE_PNG);
} finally {
if (inputStream != null) {
inputStream.close();
}
}
尽管路径中存在文件,但无法获取该文件。从Struts2.2.1升级到struts2.3.5
答案 0 :(得分:0)
如果您正在服务器上运行此代码并使用相对路径,则文件应位于服务器目录内。像:-
your_server_path/pages/appsresponse/images
问题是您要将图像保留在工作区中,需要将其保留在服务器目录中。
编辑
验证路径
File file=new File(path);
System.out.print(file.getAbsolutePath()); //check whether file is present at this path or not