1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @GetMapping("/image/{path}") public void getImage(@PathVariable String path, HttpServletResponse response) throws IOException { System.out.println("path的值:" + path); System.out.println("开始加载图片" + path); try { String imagePath = "./uploads/" + path; System.out.println(imagePath); File imageFile = new File(imagePath);
InputStream inputStream = new FileInputStream(imageFile); byte[] imageBytes = new byte[(int) imageFile.length()]; inputStream.read(imageBytes); inputStream.close();
response.setContentType(MediaType.IMAGE_JPEG.toString()); response.setContentLength(imageBytes.length); Files.copy(imageFile.toPath(), response.getOutputStream());
} catch (IOException e) { e.printStackTrace(); } }
|