java Base64 InputStream 转字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11472731/
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-10-31 05:16:18  来源:igfitidea点击:

Base64 InputStream to String

javabase64

提问by Blanca Hdez

I have been trying to get an input stream reading a file, which isa plain text and has embeded some images and another files in base64and write it again in a String. But keeping the encoding, I mean, I want to have in the String something like:

我一直试图让一个输入流读取一个文件,它是一个纯文本,并嵌入了一些图像和另一个文件,base64并再次写入一个字符串。但是保持编码,我的意思是,我想在 String 中有类似的东西:

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIf
IiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDhwQEBw7KCIoOzs7Ozs7

I have been trying with the classes Base64InputStreamand more from packages as org.apache.commons.codecbut I just can not fiugure it out. Any kind of help would be really appreciated. Thanks in advance!

我一直在尝试使用Base64InputStream包中的类和更多内容,org.apache.commons.codec但我无法弄清楚。任何形式的帮助将非常感激。提前致谢!

Edit

编辑

Piece of code using a reader:

使用阅读器的一段代码:

 BufferedReader br= new BufferedReader(new InputStreamReader(bodyPart.getInputStream()));
                        StringBuilder sb = new StringBuilder();
                        String line;
                        while ((line = br.readLine()) != null) {
                            sb.append(line);
                        }
                        br.close();

Getting as a result something like: .DIC;??C;("(;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;?à@@"??

结果如下: .DIC;??C;("(;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;?à@@"??

采纳答案by Blanca Hdez

I found the solution, inspired by this post getting base64 content string of an image from a mimepart in Java

我找到了解决方案,灵感来自这篇文章从 Java 中的 mimepart 获取图像的 base64 内容字符串

I think it is kind of stupid decode and encode again the base64 code, but it is the only way I found to manage this issue. If someone could give a better solution, it would be also really appreciated.

我认为这是一种愚蠢的解码和再次编码 base64 代码,但这是我发现管理此问题的唯一方法。如果有人可以提供更好的解决方案,也将不胜感激。

Thanks

谢谢

回答by sufinawaz

Have you tried doing this:

你有没有试过这样做:

final byte[] bytes64bytes = Base64.encodeBase64(IOUtils.toByteArray(is)); final String content = new String(bytes64bytes);

final byte[] bytes64bytes = Base64.encodeBase64(IOUtils.toByteArray(is)); final String content = new String(bytes64bytes);

回答by Denys Séguret

A text file containing some base64 data can be read with the charset of the rest of the file.

可以使用文件其余部分的字符集读取包含一些 base64 数据的文本文件。

Base64 encodingis a mean to encode bytes in a limited set of characters that are unchanged with almost all char encodings, for example ASCII or UTF-8.

Base64 编码是一种在有限的字符集中对字节进行编码的方法,这些字符几乎与所有字符编码(例如 ASCII 或 UTF-8)都保持不变。

Base64 isn't a charset encoding, you don't have to specify you have some base64 encoded data when reading a file into a string.

Base64 不是字符集编码,在将文件读入字符串时,您不必指定您有一些 base64 编码的数据。

So if your text file is generally UTF-8 (that's probable), you can read it without problem even if it contains a base64 encoded stream. Simply use a basic reader and don't use a Base64InputStream if you don't want to decode it.

因此,如果您的文本文件通常是 UTF-8(很有可能),那么即使它包含 base64 编码流,您也可以毫无问题地读取它。如果您不想解码,只需使用基本阅读器,不要使用 Base64InputStream。

When opening a file with a reader, you have to specify the encoding. If you don't know it, I suggest you test with the probable ones, like UTF-8, US-ASCIIor ISO-8859-1.

使用阅读器打开文件时,必须指定 encoding。如果你不知道,我建议你用可能的来测试,比如UTF-8,US-ASCIIISO-8859-1

回答by Badal

If you have a normal InputStream object than You can directly get Base64 encoded stream from it using apache common library class Base64InputStreamconstructor

如果您有一个普通的 InputStream 对象,您可以使用 apache 公共库类Base64InputStream构造函数直接从中获取 Base64 编码的流