无法使用java将图像转换为base64

巴拉特

我想将图像转换为base64 String使用 java。

我尝试使用以下代码

FileInputStream fileInputStreamReader = new FileInputStream(imagePath);
byte[] bytes = new byte[(int)imagePath.length()];
fileInputStreamReader.read(bytes);

String encodedFile = Base64.getEncoder().encodeToString(bytes);

但它返回一个非常小的字符串,它不是图像。

请查看我得到的 base64 字符串。

iVBORw0KGgoAAAANSUhEUgAABQAAAANVCAIAAACoFcTeAACAAElEQVR42nydBXQcR9a2Y0saZtAwg2AkzYhlWZaZGRKjmJkZLMnMIDPJFCeO7YDtOEbZlswQ2mR590t2s5hv4VtKNqT/Vt2e1tjZ/X3e06en1dNdXV09rqfeW7df2BTt2A==

请帮助我。谢谢。

Manh Quyet Nguyen
byte[] bytes = new byte[(int)imagePath.length()];

在这里你得到一个长度等于你的字节数组imagePath.length,它最多有几百个字节(imagePathyour/path/to/image.png......)

相反,您必须从imageunder获取所有字节imagePath

byte[] bytes = Files.readAllBytes(Paths.get(imagePath));
String encodedFile = Base64.getEncoder().encodeToString(bytes);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章