如何在Spring mvc项目中获取图像文件路径?

时间:2017-02-21 01:37:52

标签: java spring-mvc

package explorer

我的问题是我无法在spring项目中从资源文件夹到主包获取图像路径。我的代码如下。

  for (int i = 0; i < SUITE; i++) {
        for(int j = 0; j < FACE ; j++){

            Card card = new Card();
            BufferedImage temp = null;
            Resource resource = new FileSystemResource("src/main/webapp/resources/img/cards/" + faces[j] + "_of_" + suite[i] + ".png");
            card.setSuite(suite[i]);
            card.setFace(faces[j]);
            card.setValue(values[j]);
            card.setPath(resource.getFilename());

            try {
                temp = ImageIO.read(new File(card.getPath()));
            } catch (IOException e) {
                e.printStackTrace();
            }
            card.setImg(temp);
            deck.add(card);
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试以下

Resource resource = new ClassPathResource("img/cards/" + faces[j] + "_of_" + suite[i] + ".png");

您也可以参考Spring Framework Resource