问题
Iam using Maven and Iam trying to download images into a folder inside src/main/resources/imgs
Iam unable to download images inside 'imgs' folder , I tried below ways like:
a) System.getProperty("user.dir")
b) getClass().getClassLoader().getResourceAsStream("imgs");
c) getClass().getResourceAsStream("imgs")
BUT none of above working for me.
Please suggest me how do I able to store images inside 'src/main/resources/imgs'
回答1:
You should be trying to store images into a location that won't be available at runtime. The src
folder won't exist once your application is built and bundled.
You have lots of options available to you...
You Could...
Create a directory in user.home
and store the images there, but this does tend to clutter things up...
String format = ...
String home = System.getProperty("user.home");
String path = home + File.separator + "downloadedImages";
File fPath = new File(fPath);
if (fPath.exists() || fPath.mkdirs()) {
File imageFile = new File(fPath, "image");
BufferedImage img = ImageIO.read(...);
ImageIO.write(img, format, imageFile);
}
You Could...
Create a temporary file using something like createTempFile(String prefix, String suffix), which will allow to you to write the image out and then read it back it when you need it...
For example...
String format = ...;
File tmp = File.createTempFile("name", "img");
BufferedImage img = ImageIO.read(...); // Download the image...
ImageIO.write(img, format, tmp); // Write the image to the tmp folder...
// Deal with the image as you need...
来源:https://stackoverflow.com/questions/21978171/how-do-i-able-to-store-images-inside-src-main-resources-imgs