通讯录格式:vcf通讯录格式解析

工具类


package hd.com.xposeddemo.utils;import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;/** *@author:chendd*@date:2017/1/20*@description:解析电话薄VCF文件ENCODING=QUOTED-PRINTABLE编码代码如下 */public class CallingCardUtil { static public class ContactVcf { String eName; String cName; List<String > phoneNumbers; List<String > notes; public ContactVcf() { eName = ""; cName =""; phoneNumbers = new ArrayList<String>(); notes =new ArrayList<String>(); } } /* * 解码 */ public static 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(), "UTF-8"); } catch (Exception e) { e.printStackTrace(); return ""; } }/* * 编码 */ public static String qpEncodeing(String str) { char[] encode = str.toCharArray(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < encode.length; i++) { if ((encode[i] >= '!') && (encode[i] <= '~') && (encode[i] != '=') && (encode[i] != '\n')) { sb.append(encode[i]); } else if (encode[i] == '=') { sb.append("=3D"); } else if (encode[i] == '\n') { sb.append("\n"); } else { StringBuffer sbother = new StringBuffer(); sbother.append(encode[i]); String ss = sbother.toString(); byte[] buf = null; try { buf = ss.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } if (buf.length == 3) { for (int j = 0; j < 3; j++) { String s16 = String.valueOf(Integer.toHexString(buf[j])); // 抽取中文字符16进制字节的后两位,也就是=E8等号后面的两位, // 三个代表一个中文字符 char c16_6; char c16_7; if (s16.charAt(6) >= 97 && s16.charAt(6) <= 122) { c16_6 = (char) (s16.charAt(6) - 32); } else { c16_6 = s16.charAt(6); } if (s16.charAt(7) >= 97 && s16.charAt(7) <= 122) { c16_7 = (char) (s16.charAt(7) - 32); } else { c16_7 = s16.charAt(7); } sb.append("=" + c16_6 + c16_7); } } } } return sb.toString(); } static public CallingCardUtil.ContactVcf str2ContactVcf(String vcfContent) { String[] contents =vcfContent.split("\n"); CallingCardUtil.ContactVcf contactVcf = new CallingCardUtil.ContactVcf(); for (int i =0 ;i<contents.length;i++){// System.out.println(i); String lineContent =contents[i]; if (lineContent.trim().equals("BEGIN:VCARD")){ continue; } if (lineContent.trim().equals("END:VCARD")){ continue; } if (lineContent.trim().startsWith("VERSION")){ continue; } analysisLineContent(lineContent.trim(),contactVcf);// System.out.println("当前:"+lineContent.trim()); }// System.out.println(new Gson().toJson(contactVcf)); return contactVcf; } static private void analysisLineContent(String lineContent ,CallingCardUtil.ContactVcf contactVcf) { String[] lineContentSub=null; if (lineContent.trim().startsWith("N;")){ //英文名字 lineContentSub = lineContent.split("CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;"); if (lineContentSub.length<2){ return; } contactVcf.eName =CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0]);// System.out.println("英文名:"+ CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0])); }else if (lineContent.trim().startsWith("FN;")){ //中文名字 lineContentSub = lineContent.split("CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"); if (lineContentSub.length<2){ return; } contactVcf.cName =CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0]);// System.out.println("中文文名:"+lineContentSub[1].split(";")[0]);// System.out.println("中文文名:"+ CallingCardUtil.qpDecoding(lineContentSub[1].split(";")[0]) ); }else if (lineContent.trim().startsWith("TEL;")){ //电话号码 lineContentSub = lineContent.split("CELL:"); if (lineContentSub.length<2){ return; } contactVcf.phoneNumbers.add(lineContentSub[1]); }else if (lineContent.trim().startsWith("NOTE;")){ //备注 lineContentSub = lineContent.split("ENCODING=QUOTED-PRINTABLE:"); if (lineContentSub.length<2){ return; } String note =CallingCardUtil.qpDecoding( lineContentSub[1].trim()); contactVcf.notes.add(note); } }}


测试类


package hd.com.xposeddemo.utils;import com.google.gson.Gson;import org.junit.Test;import hd.com.xposeddemo.StreamUtil;/** * Created by czg on 2016/10/10. */public class CallingCardUtilTest { @Test public void str2ContactVcfTest(){ StringBuilder stringBuilder = new StringBuilder(); String vcfContent = new String(StreamUtil.readBytesFromStream(getClass().getResourceAsStream("/contactCallingCard.vcf"))); CallingCardUtil.ContactVcf contactVcf=CallingCardUtil.str2ContactVcf(vcfContent); System.out.println(new Gson().toJson(contactVcf)); } @Test public void qpDecodingTest() { String s =CallingCardUtil.qpDecoding("=E4=BA=B2=E4=BA=BA=E8=94=A1=E5=B0=91=E4=BF=8A"); System.out.println(s); s =CallingCardUtil.qpDecoding("=E4=BA=B2=E4=BA=BA=E8=94=A1=E5=B0=91=E4=BF=8A"); System.out.println(s); s =CallingCardUtil.qpDecoding("=E8=B4=A2=E5=8A=A1=E7=A7=91=31=30=E6=A5=BC"); System.out.println(s); }}

相关推荐

相关文章