如何在Spring Boot中接受所有嵌套的子目录直至PathVariable?

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

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:&lt;springboot-port&gt;/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 = &quot;/images/{filePath}&quot;, method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
	public void getImage(@PathVariable(&quot;filePath&quot;) String filePath, HttpServletResponse response) throws IOException {
		response.setContentType(MediaType.IMAGE_PNG_VALUE);
		StreamUtils.copy(new ClassPathResource(&quot;images/&quot; + filePath).getInputStream(), response.getOutputStream());
	}
}

答案1

得分: 1

SHORT : 在调用URL时(无论是从浏览器、Postman还是其他方式),请将路径变量部分内的"/"替换为"%2F"。

"/"在PathVariable内的使用会导致控制器无法解析该路径变量:

当您调用 localhost:&lt;springboot-port&gt;/images/some-sub-directory/some-image.png 时,控制器尝试与 /images/{var1}/{var2} 路由匹配,其中 var1 = some-sub-directoryvar2 = 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:&lt;springboot-port&gt;/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 "/")

huangapple
  • 本文由 发表于 2020年4月7日 03:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61067975.html
匿名

发表评论

匿名网友

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

确定