You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven项目中getResourceAsStream()无法找到资源求助

Maven项目资源加载问题:图片无法从src/main/resources读取

我在基于Maven的Java项目中开发图片转ASCII艺术的小程序,之前运行正常,现在突然加载不到资源目录里的图片。图片确实放在src/main/resources下,但程序一直提示找不到文件。

错误信息

Image not found in resources: cat.png

项目文件结构

ImageConverter
└───src
    ├───main
    │   ├───java
    │   │   └───(我的包)
    │   └───resources
    │       └───cat.png
    └───test

相关Image类代码

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class Image {
    // Reference https://paulbourke.net/dataformats/asciiart/
    private static final String DEFAULTIMAGE = "cat.png";
    private static final String ASCIICHARS = ".'`^\\\",:;Il!i><~+_-?][}{1)(|\\\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$";
    private static final double MAX_LUMINANCE = 196964.0;
    private BufferedImage img;

    public Image(){
        this(DEFAULTIMAGE);
    }

    public Image(String filename) {
        try {
            URL input = getClass().getClassLoader().getResource(filename);
            //InputStream input = Image.class.getResourceAsStream(filename);
            System.out.println(input);
            if (input==null) {
                System.err.println("Image not found in resources: " + filename);
                return;
            }
            this.img = ImageIO.read(input);
        } catch (IOException e){
            System.err.println("Error loading image: " + filename);
            e.printStackTrace();
        }
    }

    public BufferedImage getImg(){
        return this.img;
    }

    public String toString(){
        char[][] ASCIItable = convertToASCII(this.img);
        StringBuilder s = new StringBuilder();
        for (char[] table : ASCIItable){
            for (char cell : table){
                s.append(cell);
            }
            s.append("\n");
        }
        return s.toString();
    }

    public char[][] convertToASCII(BufferedImage img){
        FastRGB pixels = new FastRGB(img);
        char[][] ASCII_array = new char[img.getWidth()][img.getHeight()];

        for (int y = 0; y < img.getHeight(); y++){
            for (int x = 0; x < img.getWidth(); x++){
                short[] currentPixel = pixels.getRGB(x, y);
                double r = currentPixel[0];
                double g = currentPixel[1];
                double b = currentPixel[2];

                double rr = Math.pow(r, 2.2);
                double gg = Math.pow(g, 2.2);
                double bb = Math.pow(b, 2.2);

                double luminance = rr * 0.2126 + gg * 0.7152 + bb * 0.0722;
                char luminanceChar = luminanceToASCII(luminance);
                ASCII_array[x][y] = luminanceChar;
            }
        }
        return ASCII_array;
    }

    public char luminanceToASCII(double n){
        n = ((n / MAX_LUMINANCE) * ASCIICHARS.length()) - 1;
        if (n < 0)
            n = 0;
        return ASCIICHARS.charAt((int)n);
    }
}

打印出的URL input结果为null,我已尝试以下方法:

  • 确认cat.png直接位于resources文件夹内
  • 确保IDE中已将src/main/resources标记为资源根目录
  • 改用getClass().getClassLoader().getResource(filename)方法加载
  • 使用InputStream和File对象尝试加载
  • 测试同一文件夹下的3张不同图片
  • 尝试用文件系统绝对路径加载图片

后悔没建git仓库,不然至少能回退到可用状态...求各位给点建议,希望我没忽略什么显而易见的点。

内容的提问来源于stack exchange,提问作者skippa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 02:50:22