以下是根据apache-jcommons里的一组方法改写的一个方法,使用它可以用来解quoted-printable编码的字符串,类似
=b9=e3=b6=ab=d2=bb=ba=da=cd=f8=b0=c9=c9=ee=d2=b9=c6=f0=bb=f0=b4=
=f3=bb=f0 4=c8=cb=d4=e1=c9=ed=bb=f0=ba=a3
这种,一般常用在邮件中,ie保存的mht文件中也使用了这种编码。在网上很难搜索到java实现,所以在此提供。
public final string qpdecoding(string str)
{
if (str == null)
{
return "";
}
try
{
str = str.replaceall("=/n", "");
byte[] bytes = str.getbytes("us-ascii");
for (int i = 0; i < bytes.length; i++)
{
byte b = bytes[i];
if (b != 95)
{
bytes[i] = b;
}
else
{
bytes[i] = 32;
}
}
if (bytes == null)
{
return "";
}
bytearrayoutputstream buffer = new bytearrayoutputstream();
for (int i = 0; i < bytes.length; i++)
{
int b = bytes[i];
if (b == '=')
{
try
{
int u = character.digit((char) bytes[++i], 16);
int l = character.digit((char) bytes[++i], 16);
if (u == -1 || l == -1)
{
continue;
}
buffer.write((char) ((u << 4) + l));
}
catch (arrayindexoutofboundsexception e)
{
e.printstacktrace();
}
}
else
{
buffer.write(b);
}
}
return new string(buffer.tobytearray(), "gbk");
}
catch (exception e)
{
e.printstacktrace();
return "";
}
}
闽公网安备 35060202000074号