Servlet:如何构建绝对路径
我有一个java对象,并且正在尝试做这样的事情:
InputStream is = new FileInputStream(imagePath)
BufferedImage image = ImageIO.read(is)
问题是我不知道如何获取imagePath
的值。我相信我需要构建文件的绝对路径。当我当前位于 Java 类中,但我的图像文件位于 webRoot 文件夹下时,这怎么可能?我已经尝试过 getResource(".").getPath() ,但路径似乎包含其中的 .war ,我认为这是不可能的。任何帮助将不胜感激!
谢谢!
I hava a java object, and am trying to do something like this:
InputStream is = new FileInputStream(imagePath)
BufferedImage image = ImageIO.read(is)
The problem is that I do not know how to get the value of imagePath
. I believe I need to build an absolute path to the file. How is this possible when I am currently in a Java class, but my image file is under my webRoot folder? I have tried getResource(".").getPath()
but the path seems to include the .war
in it which I dont think is possible. Any help will be greatly appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们总是使用这种方式来获取一个
InputSteam
:如果你确实需要一个文件的路径,你也可以这样编码:
We always use this way to get an
InputSteam
:And if you really need the path of a file, you can also code like this:
您应该能够使用该资源,不需要路径。请参阅此 SOq:
You should be able to use the resource, you don't need the path. See this SOq: