SpringMVC 访问接口显示图片

这里通过前端传的 path 获取图片文件实际路径并予以显示,主要还是 setContentType 设置为图片格式,让浏览器感知这是一张图片。

SpringMVC 访问接口显示图片
    @RequestMapping(value = "/showImg")
    public void show(String path, HttpServletRequest request, HttpServletResponse response)  {response.setContentType("image/jpeg/jpg/png/gif/bmp/tiff/svg");
        try {path = request.getSession().getServletContext().getRealPath("/uploads/") + path;
            path = new String(path.getBytes(), "UTF-8");
            File file = new File(path);
            if (file.exists()) {InputStream in = new FileInputStream(path);
                ServletOutputStream os = response.getOutputStream();
                byte[] b = new byte[1024];
                while (in.read(b) != -1) {os.write(b);
                }
                in.close();
                os.flush();
                os.close();}
        } catch (IOException e) {e.printStackTrace();
        }
    }

手机扫描二维码访问

    本文标题:《SpringMVC 访问接口显示图片》作者:极四维博客
    原文链接:https://cway.top/post/927.html
    特别注明外均为原创,转载请注明。

    分享到微信

    扫描二维码

    可在微信查看或分享至朋友圈。

    相关文章

    发表评论:

    ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

    «    2025年5月    »
    1234
    567891011
    12131415161718
    19202122232425
    262728293031

    搜索

    控制面板

    您好,欢迎到访网站!
      查看权限

    最新留言

    文章归档

    • 订阅本站的 RSS 2.0 新闻聚合