Java implementation of inputsteam to Base64

1 inputsteam to Base64

    /**
     * InputStream to Base64
     *
     * @param inputStream
     * @return
     */
    public static String toBase64(InputStream inputStream) {
        try {
            //switch to base64
            byte[] bytes = IOUtils.toByteArray(inputStream);
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

2 debugging code

    /**
     * get Base64
     *
     * @return
     * @throws IOException
     */
    @GetMapping("/getBase64")
    public String getBases64() throws IOException {
        InputStream inputStream = new ClassPathResource("/img/logo.jpg").getInputStream();
        return toBase64(inputStream);
    }

3 picture address

4 debugging results


Read More: