从spring-MVC的src/main/resources/..中读取资源

huangapple go评论80阅读模式
英文:

Reading resources from src/main/resources/.. in spring-MVC

问题

Q1. 我在尝试捕获异常的try-catch代码块中,如果请求参数错误或请求参数image_name的文件名不存在,添加了noimage.png以供返回。但似乎不起作用,它给我一个日志,显示类路径资源[images/stores/noima.png]无法打开,因为它不存在(如果您需要完整的堆栈跟踪信息,请在下方评论中提供)。

Q2. 我在/resources/images/stores/文件夹中有两个图像文件,hello.pngnoimage.png。我可以正确读取noimage.png,但如果我发出请求localhost:8080/ImageStore.do?image_name=hello.png,那么会出现错误,生成与Q1中相同的日志。

英文:

I have a question that makes my head ache.
First, my project structure looks like below.
从spring-MVC的src/main/resources/..中读取资源

I made a controller, which returns image(*.png) file to the appropriate request.

The code of controller is written below.

@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);
    }
}

Q1. I added try-catch phrase to send noimage.png if the request parameter is wrong, or if the filename of request parameter image_name does not exist. But it doesn't seem to work, and it gives me log saying
class path resource [images/stores/noima.png] cannot be opened because it does not exist
(If you need to know the full stack trace, I will comment below.)

Q2. I have 2 image files, hello.png and noimage.png in the folder /resources/images/stores/. I can read noimage.png correctly, but if I make request localhost:8080/ImageStore.do?image_name=hello.png, then it makes an error, making the same log in Q1.

答案1

得分: 1

There's no reason to think that the constructor would result in a null value.

The exception you are getting is likely from the getInputStream method, which is documented to throw:

FileNotFoundException - if the underlying resource doesn't exist

IOException - if the content stream could not be opened

A slight adjustment might help

@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);
    }
}

You should include the stack trace, and exception message, which might assist understanding your second query, but I would check that the file really does exist, with the exact name you're using.

英文:

There's no reason to think that the constructor would result in a null value.

The exception you are getting is likely from the getInputStream method, which is documented to throw

> FileNotFoundException - if the underlying resource doesn't exist
>
> IOException - if the content stream could not be opened

A slight adjustment might help

@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);
    }
}

You should include the stack trace, and exception message, which might assist understanding your second query, but I would check that the file really does exist, with the exact name you're using.

huangapple
  • 本文由 发表于 2020年8月11日 18:48:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63356605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定