【问题标题】:How do I able to store images inside 'src/main/resources/imgs'?如何将图像存储在“src/main/resources/imgs”中?
【发布时间】:2014-03-25 13:11:20
【问题描述】:

我正在使用 Maven,我正在尝试将图像下载到 src/main/resources/imgs 内的文件夹中

我无法在“imgs”文件夹中下载图片,我尝试了以下方法:

a) System.getProperty("user.dir")

b) getClass().getClassLoader().getResourceAsStream("imgs");

c) getClass().getResourceAsStream("imgs")

但以上都不适合我。

请建议我如何将图像存储在里面 'src/main/resources/imgs'

【问题讨论】:

  • 在 maven 中,你不应该,它们应该存储在 resources 目录中。构建和部署应用程序后,src 目录将不再存在...
  • 不不,我不将图像直接存储在资源文件夹中,而是下载的图像应该直接进入文件夹'src/main/resources/imgs'..请理解我的查询!
  • 不要将文件下载到您的类路径目录中。为此选择一个不同的专用目录。
  • 你所说的“下载”是什么意思 - 你知道当你的应用程序被构建和捆绑时src 将不存在吗?
  • @SotiriosDelimanolis 你能说得简短些吗?我的意思是使用什么代码将图像存储在另一个目录中?

标签: java maven maven-2 pom.xml


【解决方案1】:

您应该尝试将图像存储到运行时不可用的位置。一旦您的应用程序被构建和捆绑,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...

【讨论】:

  • 我只需要在我的应用程序中的某个文件夹中下载图像,而不是在我的 PC 中的其他位置。所以,请告诉我最好的存储地点是哪里? (我的是 Maven 应用程序)
  • 那么,您正在尝试将嵌入在您的应用程序中的图像提取到某个外部位置??
猜你喜欢
  • 2015-04-18
  • 2014-06-26
  • 1970-01-01
  • 2015-03-22
  • 2014-09-25
  • 2016-08-22
  • 1970-01-01
  • 2019-01-08
  • 2017-12-03
相关资源
最近更新 更多