服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java的quoted-printable解码器源代码


  以下是根据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 "";
  }
 }

扫描关注微信公众号