The following code grants me access to a specific PNG resource within my project:
BufferedImage temp = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("play.png"));
However, I have n images that I don't have to have to hardcode ("play.png", "pause.png", etc); without having an external image library (which I would simply loop through with File
), is there a way to get all images within the project?
Ideally, all files within a specific source folder, really. Or source folders. getResources
only seems to work for multiples of a specific named resource, which isn't what I want.
EDIT :
To be clear, this is a local project / application; it isn't intended to be a servlet or otherwise deployed on the Web. Question updated with the offline
tag in case this is applicable.
You can do something like this.Here I am trying to get all the images in a folder named images
in a servlet.
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String pathToImages = getServletContext().getRealPath("/images");
File[] files = new File(pathToImages).listFiles();
showImages(files);
}
public static void showImages(File[] files) {
for (File file : files) {
System.out.println("File: " + file.getName());
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.