如何在"src/main/resources/imgs"中存储图像? [英] How do I able to store images inside 'src/main/resources/imgs'?

查看:93
本文介绍了如何在"src/main/resources/imgs"中存储图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Iam使用Maven,而Iam尝试将图像下载到src/main/resources/imgs

Iam using Maven and Iam trying to download images into a folder inside src/main/resources/imgs

我无法下载'imgs'文件夹中的图像,我尝试了以下方式:

Iam unable to download images inside 'imgs' folder , I tried below ways like:

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

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

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

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

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

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

但以上都不对我有用.

请建议我如何在其中存储图像 'src/main/resources/imgs'

推荐答案

您应该尝试将图像存储到运行时不可用的位置.构建并捆绑了应用程序后,src文件夹将不存在.

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...

user.home中创建一个目录并将图像存储在此处,但这确实会使事情变得混乱...

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...

例如...

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...

这篇关于如何在"src/main/resources/imgs"中存储图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆