英文:
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.png
和noimage.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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论