springMVC读取本地图片显示到前端页面

    @RequestMapping("/getImage")
    @ResponseBody
    public void getImagesId(HttpServletResponse rp) {
        String filePath = "G:\1.jpg";
        File imageFile = new File(filePath);
        if (imageFile.exists()) {
            FileInputStream fis = null;
            OutputStream os = null;
            try {
                fis = new FileInputStream(imageFile);
                os = rp.getOutputStream();
                int count = 0;
                byte[] buffer = new byte[1024 * 8];
                while ((count = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, count);
                    os.flush();
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

方法一:在浏览器输入请求地址,就可以看到本地的图片

方法二:在jsp中使用<img src=“请求地址” />,也可以看到本地的图片

原文地址:https://www.cnblogs.com/jiefu/p/10900579.html