Java – ServletContext getresource does not work
•
Java
I tried to use ServletContext Getresource to retrieve the Java net. URL reference (then I'll include it in the PDF library using iText)
Example 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
Example 2:
URL pictureURL = ServletContext.getResource("picture.jpg"); // pictureURL is always null
Then build a Java. Net file that points to the files in my webapps / folder net. What is the correct method for URL objects? Why does getrealpath work but not getresource?
In case of help, here is my folder structure
webapps -> mySite -> picture.jpg
My pictures need to be stored in WEB-INF or WEB-INF / classes before they can be read by getresource?
Solution
Therefore, you must provide a full path relative to the context For example:
URL pictureURL = servletContext.getResource("/images/picture.jpg");
(note the ServletContext variable below)
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码