Java inputStream=新文件inputStream(路径)不工作
这是Java类中的代码。 redflaglight.png图像显示在“图像”文件夹中 文件未找到空指针异常位于 inputStream=新文件InputStreamPath; Struts2.3.5 Java7Java inputStream=新文件inputStream(路径)不工作,java,struts2,Java,Struts2,这是Java类中的代码。 redflaglight.png图像显示在“图像”文件夹中 文件未找到空指针异常位于 inputStream=新文件InputStreamPath; Struts2.3.5 Java7 public String createRootPath() throws UnsupportedEncodingException { String rootPath = ""; String path = Expor
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后开始的
若您在服务器上运行此代码并使用相对路径,那个么您的文件应该位于服务器目录中。比如:-
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
不要在web应用程序中使用绝对路径我只使用相对路径。。。为了让您清楚地了解本地i have posted:/480499_中的值返回的内容,工作区是一个绝对路径!还发布了一个。请描述一下你想做什么,请投入一点精力,把所有的代码都放在你的问题中。让我知道如何让它在本地工作。因为服务器目录中的文件已经放好了。你到底在问什么,我不明白。尽管up_arrow_export.png可用,但它找不到。这是从struts2.2.1升级到struts2.3后开始的。5@InduKumar在post中检查编辑