英文:
How to accept all nested sub-directories up to PathVariable in Spring Boot?
问题
Background
我正在尝试访问位于 classpath:resources/images
中的图片。图像目录中包含子目录。
Issue
当访问例如 localhost:<springboot-port>/images/some-sub-directory/some-image.png
时,我收到以下错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Apr 06 19:52:20 UTC 2020
There was an unexpected error (type=Not Found, status=404).
SRVE0295E: Error reported: 404
我如何以这种方式访问子目录中的图片?
Code
@RestController
public class ImagesController {
@CrossOrigin
@RequestMapping(value = "/images/{filePath}", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public void getImage(@PathVariable("filePath") String filePath, HttpServletResponse response) throws IOException {
response.setContentType(MediaType.IMAGE_PNG_VALUE);
StreamUtils.copy(new ClassPathResource("images/" + filePath).getInputStream(), response.getOutputStream());
}
}
英文:
Background
I am trying to access images contained within classpath:resources/images
. There are sub-directories contained within the images directory.
Issue
When accessing, for example, localhost:<springboot-port>/images/some-sub-directory/some-image.png
I am presented with the error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Apr 06 19:52:20 UTC 2020
There was an unexpected error (type=Not Found, status=404).
SRVE0295E: Error reported: 404
How can I access images from sub-directories in this fashion?
Code
@RestController
public class ImagesController {
@CrossOrigin
@RequestMapping(value = "/images/{filePath}", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public void getImage(@PathVariable("filePath") String filePath, HttpServletResponse response) throws IOException {
response.setContentType(MediaType.IMAGE_PNG_VALUE);
StreamUtils.copy(new ClassPathResource("images/" + filePath).getInputStream(), response.getOutputStream());
}
}
答案1
得分: 1
SHORT : 在调用URL时(无论是从浏览器、Postman还是其他方式),请将路径变量部分内的"/"替换为"%2F"。
"/"在PathVariable内的使用会导致控制器无法解析该路径变量:
当您调用 localhost:<springboot-port>/images/some-sub-directory/some-image.png
时,控制器尝试与 /images/{var1}/{var2}
路由匹配,其中 var1 = some-sub-directory
,var2 = some-image.png
,但匹配失败。
我建议您在要传递为PathVariable的字符串中找到一种编码"/"的方法,然后在方法内解码它,然后才生成所请求图像的URI(将任何用作代码的内容替换回"/")。
英文:
SHORT : replace "/" with "%2F" inside the path variable part of url when you call it (from browser or postman or whatever)
The use of "/" inside your PathVariable prevents this path variable to be parsed by controller :
When you call localhost:<springboot-port>/images/some-sub-directory/some-image.png
the controller tries to match with /images/{var1}/{var2}
route where var1 = some-sub-directory
and var2 = some-image.png
and fails.
I suggest you find a way to encode "/" in the string to be passed as the pathvariable then you decode it inside the method and only then you make the uri of the requested image. (replacing back whatever you uses as a code with "/")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论