json格式转base64编码去除多余的换行

  • Post author:
  • Post category:其他




json格式转base64编码后自带换行问题


问题

:json格式转base64编码格式后自带换行,导致转换后图片无法打开,转换出来的base64编码如下图。

在这里插入图片描述

很明显,传过来的base64编码格式数据明显出现换行,原因统一编码格式utf-8,再通过json传输过程中会出现base64自带换行。

**解决方法:**使用去掉”\\r\\n”去除多余的换行。


参考Java代码:

public static void main(String[] args) {
        String base64 = null;
        try {
            File file = new File("C:/桌面/cc.txt");  //此处cc.txt是本地带换行的base64文件
            FileInputStream fis = new FileInputStream(file);
            byte[] byte64 = new byte[(int) file.length()];
            fis.read(byte64);
            base64 = new String(byte64);
            String str = base64.replace("\\r\\n", "");
            System.out.print(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


备注:


需要base64编码文件可以在线上上传文件进行转换。

参考链接:https://www.zhangxinxu.com/sp/base64.html

直接拖文件进去即可。

base64,后面的即是图片转码后值,正常的base64无换行。如下图:

在这里插入图片描述



版权声明:本文为CAIXIONGSHUN原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。