因为需要对接第三方接口,需要将图片转换为base64编码传参。
手动转换base64使用postman完全是OK的。结果java中转换出来死活不行。
/**
* <p>将文件转成base64 字符串</p>
* @param path 文件路径
* @return
* @throws Exception
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer).replaceAll("\\n", "").replaceAll("\\r","");
}
暂时不知道为什么转换出来后会有一些转义字符。