如何在java中将图像转换为base64字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36492084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:55:57  来源:igfitidea点击:

How to convert an Image to base64 string in java?

javaimagebase64

提问by Setu Kumar Basak

It may be a duplicate but i am facing some problem to convert the image into Base64for sending it for Http Post. I have tried this code but it gave me wrong encoded string.

它可能是重复的,但我在将图像转换Base64为用于发送时遇到了一些问题Http Post。我试过这段代码,但它给了我错误的编码字符串。

 public static void main(String[] args) {

           File f =  new File("C:/Users/SETU BASAK/Desktop/a.jpg");
             String encodstring = encodeFileToBase64Binary(f);
             System.out.println(encodstring);
       }

       private static String encodeFileToBase64Binary(File file){
            String encodedfile = null;
            try {
                FileInputStream fileInputStreamReader = new FileInputStream(file);
                byte[] bytes = new byte[(int)file.length()];
                fileInputStreamReader.read(bytes);
                encodedfile = Base64.encodeBase64(bytes).toString();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return encodedfile;
        }

Output:[B@677327b6

But i converted this same image into Base64in many online encoders and they all gave the correct big Base64 string.

输出:[B@677327b6

但我Base64在许多在线编码器中转换了相同的图像,它们都给出了正确的大 Base64 字符串。

Edit:How is it a duplicate?? The link which is duplicate of mine doesn't give me solution of converting the string what i wanted.

编辑:它是如何重复的?我的重复链接没有给我转换我想要的字符串的解决方案。

What am i missing here??

我在这里错过了什么?

采纳答案by Lolo

The problem is that you are returning the toString()of the call to Base64.encodeBase64(bytes)which returns a byte array. So what you get in the end is the default string representation of a byte array, which corresponds to the output you get.

问题是您正在返回返回字节数组toString()的调用的Base64.encodeBase64(bytes)。所以你最终得到的是一个字节数组的默认字符串表示,它对应于你得到的输出。

Instead, you should do:

相反,你应该这样做:

encodedfile = new String(Base64.encodeBase64(bytes), "UTF-8");

回答by MojioMS

this did it for me. you can vary the options for the output format to Base64.Default whatsoever.

这是为我做的。您可以将输出格式的选项更改为 Base64.Default。

// encode base64 from image
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
encodedString = Base64.encodeToString(b, Base64.URL_SAFE | Base64.NO_WRAP);

回答by Joels Elf

I think you might want:

我想你可能想要:

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