Java 如何获取图像绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3205503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get image absolute path?
提问by Rose
How to get image path in java ?
如何在java中获取图像路径?
I am using eclipse, i want to display the image in jsp,I want to give path like "/images/logo.jpg" but it is getting nullpointer exception when execute the page.
我正在使用 eclipse,我想在 jsp 中显示图像,我想给出像“/images/logo.jpg”这样的路径,但是在执行页面时出现空指针异常。
if i give fullpath , it is working like "d:/project/images/logo.jsp".How to get display the image with absolute path ?
如果我给出 fullpath ,它就像“d:/project/images/logo.jsp”一样工作。如何使用绝对路径显示图像?
回答by Eugene Ryzhikov
Instead of /images/logo.jpg
put images/logo.jpg
i.e. remove the first slash.
而不是/images/logo.jpg
put images/logo.jpg
ie 删除第一个斜杠。
回答by BalusC
You should avoid using relative paths in java.io
stuff as much as possible. Any relative path will be relative to the current working directory which is dependent on the way how you started the application and is uncontrollable from inside the code. When started as e.g. a Tomcat service, it will be relative to c:/path/to/tomcat/bin
. When running in Eclipse, it will be relative to c:/path/to/eclipse/project/bin
. When running in command console, it will be relative to currently opened folder. Etcetera. You don't want to be dependent on that. Bad idea.
您应该尽可能避免在java.io
内容中使用相对路径。任何相对路径都将相对于当前工作目录,这取决于您启动应用程序的方式,并且无法从代码内部进行控制。当作为 Tomcat 服务启动时,它将相对于c:/path/to/tomcat/bin
. 在 Eclipse 中运行时,它将相对于c:/path/to/eclipse/project/bin
. 在命令控制台中运行时,它将相对于当前打开的文件夹。等等。你不想依赖它。馊主意。
In case of JSP/Servlet webapplications there are basically two ways to obtain an absolute resource path using a relative path in a reliable way:
在 JSP/Servlet webapplications 的情况下,基本上有两种方法可以使用相对路径以可靠的方式获取绝对资源路径:
Retrieve it from the runtime classpath(there where all the classes and libraries are):
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String path = classLoader.getResource("/images/logo.jpg").getPath();
Retrieve it from the webcontent(there where all the JSP files and
/WEB-INF
folder are):ServletContext context = getServletContext(); // Inherited from HttpServlet. String path = context.getResource("/images/logo.jpg").getPath();
从运行时类路径中检索它(所有类和库都在那里):
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); String path = classLoader.getResource("/images/logo.jpg").getPath();
从webcontent 中检索它(所有 JSP 文件和
/WEB-INF
文件夹都在那里):ServletContext context = getServletContext(); // Inherited from HttpServlet. String path = context.getResource("/images/logo.jpg").getPath();
If all you ultimately want is an InputStream
out of it, which you perhaps intented to create using new FileInputStream(path)
, then you should be using getResourceAsStream()
methods instead:
如果您最终想要的只是一个InputStream
out of it,您可能打算使用创建它new FileInputStream(path)
,那么您应该使用getResourceAsStream()
方法:
From classpath:
InputStream content= classLoader.getResourceAsStream("/images/logo.jpg");
Or webcontent:
InputStream content = context.getResourceAsStream("/images/logo.jpg");
从类路径:
InputStream content= classLoader.getResourceAsStream("/images/logo.jpg");
或网页内容:
InputStream content = context.getResourceAsStream("/images/logo.jpg");