【问题标题】:Reading resources from src/main/resources/.. in spring-MVC在 spring-MVC 中从 src/main/resources/.. 读取资源
【发布时间】:2020-08-11 10:48:43
【问题描述】:

我有一个让我头疼的问题。 首先,我的项目结构如下所示。

我制作了一个控制器,它将图像(*.png)文件返回给适当的请求。

控制器的代码如下。

@Controller
public class ImageController {

    @GetMapping(value = "/ImageStore.do", produces = MediaType.IMAGE_PNG_VALUE)
    public @ResponseBody byte[] getStoreImage(HttpServletRequest request) throws IOException {
        String image_name = request.getParameter("image_name");
        Resource resource = null;
        try {
            resource = new ClassPathResource("/images/stores/" + image_name);
            if(resource == null) {
                throw new NullPointerException();
            }
        } catch(NullPointerException e) {
            resource = new ClassPathResource("/images/stores/noimage.png");
        }
        InputStream inputStream = resource.getInputStream();
        return IOUtils.toByteArray(inputStream);
    }
}

第一季度。如果请求参数错误,或者请求参数image_name 的文件名不存在,我添加了try-catch 短语以发送noimage.png。但它似乎不起作用,它给了我日志说 class path resource [images/stores/noima.png] cannot be opened because it does not exist (如果您需要了解完整的堆栈跟踪,我将在下面发表评论。)

第二季度。我有 2 个图像文件,hello.pngnoimage.png 在文件夹 /resources/images/stores/ 中。我可以正确读取noimage.png,但如果我提出请求localhost:8080/ImageStore.do?image_name=hello.png,则会出错,在第一季度进行相同的日志。

【问题讨论】:

    标签: java spring spring-mvc model-view-controller


    【解决方案1】:

    没有理由认为构造函数会产生null 值。

    你得到的异常很可能来自getInputStream方法,也就是documented to throw

    FileNotFoundException - 如果底层资源不存在

    IOException - 如果内容流无法打开

    稍作调整可能会有所帮助

    @Controller
    public class ImageController {
    
        @GetMapping(value = "/ImageStore.do", produces = MediaType.IMAGE_PNG_VALUE)
        public @ResponseBody byte[] getStoreImage(HttpServletRequest request) throws IOException {
            InputStream is = null;
            try {
                String image_name = request.getParameter("image_name");
                is = new ClassPathResource("/images/stores/" + image_name).getInputStream();
            } catch(FileNotFoundException e) {
                is = new ClassPathResource("/images/stores/noimage.png").getInputStream();
            }
    
            return IOUtils.toByteArray(is);
        }
    }
    

    您应该包含堆栈跟踪和异常消息,这可能有助于理解您的第二个查询,但我会检查该文件是否确实存在,以及您使用的确切名称。

    【讨论】:

    • 首先,感谢@ptomli 的回复。我试过了,它似乎并没有解决我的问题。如果你看上面我的项目结构图,可以看到/resources/images/stores/下面有hello.pngnoimage.png。如果我提出请求localhost:8080/ImageStore.do?image_name=hello.png,它会向我显示正确的图像,但如果我提出localhost:8080/ImageStore.do?image_name=noimage.png,它会向我显示 FileNotFoundException 页面。 spring 是否有可能无法从资源文件夹中读取超过 1 个文件?
    • java.io.FileNotFoundException: class path resource [images/events/noimage.png] cannot be opened because it does not exist.
    • 您在此处的最后一条评论显示在events 子目录中查找文件时出错,这表明生成错误的代码与我们在上面查看的代码不同。
    • 是的,但是如果events 子目录错误,它也应该无法重新读取hello.png 文件,,
    • 我通过将noimage.png的名称重构为default.png解决了这个问题。 noimage 这个名字似乎是某种保留字之类的 :) 感谢您的帮助!
    猜你喜欢
    • 2014-04-01
    • 2015-02-26
    • 2017-02-05
    • 2020-09-13
    • 2017-02-10
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    相关资源
    最近更新 更多