Java 如何将图像存储在';src/main/resources/imgs';?
Iam正在使用Maven,Iam正在尝试将图像下载到Java 如何将图像存储在';src/main/resources/imgs';?,java,maven,maven-2,pom.xml,Java,Maven,Maven 2,Pom.xml,Iam正在使用Maven,Iam正在尝试将图像下载到src/main/resources/imgs 我无法下载“imgs”文件夹中的图像,我尝试了以下方法: a) System.getProperty(“user.dir”) b) getClass().getClassLoader().getResourceAsStream(“imgs”) c) getClass().getResourceAsStream(“imgs”) 但以上这些都不适合我 请建议我如何将图像存储在'src/main/res
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);
}
你可以。。。
使用以下内容创建一个临时文件,它允许您写出图像,然后在需要时将其读回
例如
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...
在maven中,您不应该这样做,它们应该存储在
resources
目录中。当您构建和部署应用程序时,src
目录将不再存在……不,不,我不直接将图像存储在资源文件夹中,而下载的图像应直接进入文件夹“src/main/resources/imgs”中。。请理解我的疑问!不要将文件下载到类路径目录中。为此选择一个不同的、专用的目录。你所说的“下载”是什么意思?你是否理解,当你的应用程序被构建和绑定时,src
将不存在?@SotiriosDelimanolis你能不能请点breif?我的意思是使用什么代码将图像存储在另一个目录中?我只需要将图像下载到我的应用程序中的某个文件夹中,而不是我电脑中的某个其他位置。因此,请告诉我哪里是最佳存储位置?(我的是Maven应用程序)所以,您试图将嵌入应用程序中的图像提取到某个外部位置??