一致以來都是用 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; }
演示效果: