springboot图片路径形式获取图片

一致以来都是用 http://127.0.0.1:8888/getPhoto?imgUrl=1.jpg 的形式获取数据,今天突然要 http://127.0.0.1:8888/getPhoto/1.jpg 这样获取图片数据,竟然有点懵逼了。

直接贴代码:

@RequestMapping(value = "/getPhoto/{imgUrl:[a-zA-Z0-9_.]+}", produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] getPhoto(@PathVariable("imgUrl") String imgUrl) {
    File file = new File("D:/test.jpg");
    FileInputStream inputStream = new FileInputStream(file);
    byte[] bytes = new byte[inputStream.available()];
    inputStream.read(bytes, 0, inputStream.available());
    return bytes;
}

演示效果:

image


原文地址:https://www.cnblogs.com/betterwgo/p/12878182.html