Java 如何解码可引用字符(从可引用到字符)?
我有一条短信。以下是此类文本的示例(来自a): 如果你相信真理=美,那么肯定=20=Java 如何解码可引用字符(从可引用到字符)?,java,encoding,Java,Encoding,我有一条短信。以下是此类文本的示例(来自a): 如果你相信真理=美,那么肯定=20= 数学是哲学中最美丽的分支 我正在寻找一个Java类,它将编码的表单解码为字符,例如,=20到一个空格 更新:感谢Elite先生,我知道我需要使用QuotedPrintable编解码器: import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.net.QuotedPrintableCodec; import
数学是哲学中最美丽的分支 我正在寻找一个Java类,它将编码的表单解码为字符,例如,=20到一个空格 更新:感谢Elite先生,我知道我需要使用QuotedPrintable编解码器:
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.QuotedPrintableCodec;
import org.junit.Test;
public class QuotedPrintableCodecTest {
private static final String TXT = "If you believe that truth=3Dbeauty, then surely=20=mathematics is the most beautiful branch of philosophy.";
@Test
public void processSimpleText() throws DecoderException
{
QuotedPrintableCodec.decodeQuotedPrintable( TXT.getBytes() );
}
}
但是,我不断得到以下例外情况:
org.apache.commons.codec.DecoderException: Invalid URL encoding: not a valid digit (radix 16): 109
at org.apache.commons.codec.net.Utils.digit16(Utils.java:44)
at org.apache.commons.codec.net.QuotedPrintableCodec.decodeQuotedPrintable(QuotedPrintableCodec.java:186)
我做错了什么
更新2:我发现并了解:
但是输出仍然不完美,它包含“=”:
INPUT: If you believe that truth=3Dbeauty, then surely=20= mathematics is the most beautiful branch of philosophy.
OUTPUT: If you believe that truth=beauty, then surely = mathematics is the most beautiful branch of philosophy.
现在我做错了什么?类所做的是RFC1521引用的可打印部分的实现
更新,您引用的可打印字符串是错误的,因为Wikipedia上的示例使用了软换行符 软换行符:
Rule #5 (Soft Line Breaks): The Quoted-Printable encoding REQUIRES
that encoded lines be no more than 76 characters long. If longer
lines are to be encoded with the Quoted-Printable encoding, 'soft'
line breaks must be used. An equal sign as the last character on a
encoded line indicates such a non-significant ('soft') line break
in the encoded text. Thus if the "raw" form of the line is a
single unencoded line that says:
Now's the time for all folk to come to the aid of
their country.
This can be represented, in the Quoted-Printable encoding, as
Now's the time =
for all folk to come=
to the aid of their country.
This provides a mechanism with which long lines are encoded in
such a way as to be restored by the user agent. The 76 character
limit does not count the trailing CRLF, but counts all other
characters, including any equal signs.
因此,您的文本应如下所示:
private static final String CRLF = "\r\n";
private static final String S = "If you believe that truth=3Dbeauty, then surely=20=" + CRLF + "mathematics is the most beautiful branch of philosophy.";
Javadoc明确指出:
引用的可打印规范的规则#3、#4和#5未实施
然而,因为完整引用的可打印规范本身并不适用
深入到面向字节[]的编解码器框架中。完成一次编解码器
蒸汽编解码器框架已经准备就绪。背后的动机
以部分形式提供编解码器是因为它已经可以进来了
适用于不需要引用可打印行的应用程序
格式化(规则#3、#4、#5),例如Q编解码器
还有一个Apache QuotedPrintableCodec,因为它不支持软换行。谢谢你的回答,不幸的是,当我试图从Wikipedia页面解码示例时,我遇到了一个异常。@Skarab,除非你向我们展示代码&异常堆栈跟踪,否则你不能认为我的答案是错误的,因为它引发了一个异常。您要求提供一个解码可引用字符的类&我确实向您展示了。@Skarab,我更新了我的帖子到您的家庭作业练习中。我现在应该期望+1:)
private static final String CRLF = "\r\n";
private static final String S = "If you believe that truth=3Dbeauty, then surely=20=" + CRLF + "mathematics is the most beautiful branch of philosophy.";