Java 输出 Base64 图片数据到浏览器

Java 输出 Base64 图片数据到浏览器

 @GetMapping("/thumbnail")
  public void getThumbnail(HttpServletResponse response) {
     
    	String thumbnail = "Base64数据";
        if (StringUtils.isNotBlank(thumbnail)) {
     
            try (OutputStream outputStream = response.getOutputStream();
                 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();) {
     

                response.setContentType("image/png;charset=utf-8");
                String after = StringUtils.substringAfter(thumbnail, "base64,");
                byte[] bytes = Base64.getDecoder().decode(after);

                byteArrayOutputStream.write(bytes);
                byteArrayOutputStream.writeTo(outputStream);

            } catch (Exception e) {
     
                e.printStackTrace();
            }
        }
    }

你可能感兴趣的:(工具类)