java - Spring mvc项目中如何获取图片文件路径?

标签 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);
        }
    }

最佳答案

尝试以下

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

您还可以引用Spring Framework Resource

关于java - Spring mvc项目中如何获取图片文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42357188/

相关文章:

Java 泛型 hell

java - Spring Boot Autowiring 空值

java - Spring请求映射自定义注解——二义性映射

java - Spring Web MVC Cycle中formbackingobject和referencedata对象如何操作?

java - 在 ArrayList 中创建唯一项的 ArrayList

java - 重写java中的equals方法

java - 将 HashMap 复制到另一个 HashMap

java - XX :NativeMemoryTracking Native memory tracking has been shutdown due to out of native memory

java - Spring上下文初始化(java)

java - 如何使用 Spring Security 获取 session 超时消息