我正在尝试使用它ServletContext.getResource
来检索java.net.url
对图像文件的引用(然后我将使用iText将其包含在PDF库中).
当我使用时ServletContext.getRealPath("picture.jpg")
,我得到一个字符串URL.但是,getResource始终返回null
.
例1:
String picture = ServletContext.getRealPath("picture.jpg");
// picture contains a non-null String with the correct path
URL pictureURL = ServletContext.getResource(picture);
// pictureURL is always null
Run Code Online (Sandbox Code Playgroud)
例2:
URL pictureURL = ServletContext.getResource("picture.jpg");
// pictureURL is always null
Run Code Online (Sandbox Code Playgroud)
那么构建指向我webapps/
文件夹中文件的java.net.URL对象的正确方法是什么?为什么getRealPath
工作但不是getResource
?
如果它有帮助,这是我的文件夹结构
webapps -> mySite -> picture.jpg
Run Code Online (Sandbox Code Playgroud)
我的照片是需要存储在其中WEB-INF
还是WEB-INF/classes
要被读取getResource
?
Boz*_*zho 11
返回映射到指定路径的资源的URL.路径必须以"/"开头,并被解释为相对于当前上下文根.
因此,您必须提供上下文相关的完整路径.例如:
URL pictureURL = servletContext.getResource("/images/picture.jpg");
Run Code Online (Sandbox Code Playgroud)
(注意较低的servletContext
变量)
归档时间: |
|
查看次数: |
16944 次 |
最近记录: |