我正在使用Maven,我正在尝试将图像下载到src/main/resources/imgs
内部的文件夹中
无法下载"imgs"文件夹中的图像,我尝试了以下方法,例如:
a) System.getProperty("user.dir")
b) getClass().getClassLoader().getResourceAsStream("imgs");
c) getClass().getResourceAsStream("imgs")
但以上都不适合我。
请建议我如何在'src/main/resources/imgs'
内存储图像
您应该尝试将图像存储到运行时不可用的位置。 生成并捆绑应用程序后,src
文件夹将不存在。
您有很多选择...
你可以...
在user.home
中创建一个目录并将图像存储在那里,但这确实会使事情变得混乱......
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);
}
你可以...
使用类似的东西创建一个临时文件 createTempFile(String prefix, String suffix)
,这将允许您写出图像,然后在需要时将其读回......
例如。。。
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...