[英]Reading resources from src/main/resources/.. in spring-MVC
I have a question that makes my head ache.我有一个让我头疼的问题。 First, my project structure looks like below.
首先,我的项目结构如下所示。
I made a controller, which returns image(*.png) file to the appropriate request.我制作了一个 controller,它将图像(* .png)文件返回给适当的请求。
The code of controller is written below. controller的代码写在下面。
@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. 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.如果请求参数错误,或者请求参数
image_name
的文件名不存在,我添加了try-catch
短语来发送noimage.png
。 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.)但它似乎不起作用,它给我的日志说
class path resource [images/stores/noima.png] cannot be opened because it does not exist
(如果您需要知道完整的堆栈跟踪,我将在下面评论.)
Q2. Q2。 I have 2 image files,
hello.png
and noimage.png
in the folder /resources/images/stores/
.我在文件夹
/resources/images/stores/
中有 2 个图像文件, hello.png
和noimage.png
。 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.我可以正确读取
noimage.png
,但是如果我发出请求localhost:8080/ImageStore.do?image_name=hello.png
,那么它会出错,在 Q1 中进行相同的日志。
There's no reason to think that the constructor would result in a null
value.没有理由认为构造函数会产生
null
值。
The exception you are getting is likely from the getInputStream
method, which is documented to throw您得到的异常可能来自
getInputStream
方法,该方法记录为 throw
FileNotFoundException - if the underlying resource doesn't exist
FileNotFoundException - 如果底层资源不存在
IOException - if the content stream could not be opened
IOException - 如果内容 stream 无法打开
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.您应该包括堆栈跟踪和异常消息,这可能有助于理解您的第二个查询,但我会检查该文件是否确实存在,以及您使用的确切名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.