Java:使用带引号的可打印字符串编码 [英] Java: Encode String in quoted-printable
问题描述
我正在寻找一种quoted-printable
在Java中编码字符串的方法,就像php的本机 quoted_printable_encode()
函数.
I am looking for a way to quoted-printable
encode a string in Java just like php's native quoted_printable_encode()
function.
我尝试使用JavaMails的MimeUtility库.但是我无法获得
I have tried to use JavaMails's MimeUtility library. But I cannot get the encode(java.io.OutputStream os, java.lang.String encoding)
method to work since it is taking an OutputStream as input instead of a String (I used the function getBytes()
to convert the String) and outputs something that I cannot get back to a String (I'm a Java noob :)
有人可以给我提示如何编写将字符串转换为OutputStream并在编码后将结果输出为String的包装器吗?
Can anyone give me tips on how to write a wrapper that converts a String into an OutputStream and outputs the result as a String after encoding it?
推荐答案
要使用此MimeUtility
方法,您必须创建一个ByteArrayOutputStream
,该ByteArrayOutputStream
会累积写入该字节的字节,然后可以对其进行恢复.例如,对字符串original
进行编码:
To use this MimeUtility
method you have to create a ByteArrayOutputStream
which will accumulate the bytes written to it, which you can then recover. For example, to encode the string original
:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream encodedOut = MimeUtility.encode(baos, "quoted-printable");
encodedOut.write(original.getBytes());
String encoded = baos.toString();
String encoded = MimeUtility.encodeText(original, null, "Q");
这篇关于Java:使用带引号的可打印字符串编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!