Advertisement

Base64 encoding display image—footnote note about pitfall

阅读量:

Java将图片转为base64编码

直入主题,先上代码

复制代码
    	/** * 获取图片的Base64编码字符串
     * * @param imgPath 文件路径
     * @return
     */
    public String getImgBase64(String imgPath) {
        // 将图片转为字节数组
        byte[] imgData = null;
        try {
            imgData = Files.readAllBytes(Paths.get(imgPath));
        } catch (IOException e) {
            log.error("Read file to byteArray failed. Error: {}", e);
        }
    
        // 对字节数组进行Base64编码
        String result = "";
        if (imgData != null) {
            // 使用JDK自带的Base64架包
            byte[] baseByte = Base64.getEncoder().encode(imgDat

全部评论 (0)

还没有任何评论哟~