工具类

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);

    }}

vcf通讯录格式解析相关推荐

  1. 关于vcard通讯录格式解析

    [本文转自互联网,如有涉及侵权等伤害到您的利益,请及时告知,本人将马上删除] 最在网络上面查找关于vcard格式的技术资料,发现中文的资料很少,只能阅读vCard MIME Directory Pro ...

  2. Python 将TXT格式转换为手机通讯录格式vcf

    import os import tkinter as tk from tkinter import filedialog from tkinter import messagebox#获取需要转换的 ...

  3. 【效率特工队】Excel转手机通讯录格式,如何将一个Excel批量拆分生成多个vcf,怎么快速的加微信好友,优化一下方法效率可能更高,解决微信通讯录手机联系人好友不显示的问题

    本文用到的软件下载地址 软件下载地址:https://share.weiyun.com/JG6ZNAv7 CSDN贵族下载地址:https://download.csdn.net/download/b ...

  4. 【效率特工队】记事本txt文本号码如何转换安卓苹果手机通讯录vCard(vcf)格式,最快的方法

    很多人以前用过我们的表格EXCEL还有WPS转通讯录格式VCF(vCard)的软件,苹果和安卓通用,但是有很多人在这里不会用表格,有的时候表格格式不支持,今天我们做一个记事本txt文本直接转手机通讯录 ...

  5. 不能编辑access_vcf通讯录编辑器 v3.1.6 vcf通讯录编辑器软件

    vcf通讯录编辑器是一款可以在电脑上编辑手机通讯录的通讯录备份软件,该软件体积小巧,操作简单,用户只需要将备份的通讯录文件 Contacts.vcf (或者将 vcf 文件改成这个文件名)复制到执行目 ...

  6. 手机号码转码_通讯录格式转换集锦

    通讯录格式转换集锦 随着大家生活水平的提高,更换手机已经是一件十分平常的事 情,新手机功能越来越强大, 功能越来越多,体验新手机的心情当然 是愉悦的,可是通讯录的转移和编辑(比如用 EXCEL 来编辑 ...

  7. java手机通讯录格式转换_通讯录格式转换器设计与开发(JAVA平台)

    通讯录格式转换器设计与开发(平台)(任务书,开题报告,文献综述,中期检查表,外文翻译,毕业论文17000字,程序代码) 本文从当前社会人们需求出发,首先介绍了人们在处理手机信息和网络信息交互时的极大不 ...

  8. 转:YUV RGB 常见视频格式解析

    转: http://www.cnblogs.com/qinjunni/archive/2012/02/23/2364446.html YUV RGB 常见视频格式解析 I420是YUV格式的一种,而Y ...

  9. 【Android RTMP】音频数据采集编码 ( AAC 音频格式解析 | FLV 音频数据标签解析 | AAC 音频数据标签头 | 音频解码配置信息 )

    文章目录 安卓直播推流专栏博客总结 一. AAC 音频格式解析 二. FLV 音频数据标签解析 1. 分析 FLV 格式中的 AAC 音频格式数据 2. AAC 音频特殊配置 3. AAC 音频数据标 ...

最新文章

  1. 扩散模型就是自动编码器!DeepMind研究学者提出新观点并论证
  2. numpy中矩阵运算的特点
  3. 开发日记-20190705 关键词 读书笔记 《Perl语言入门》Day 2
  4. 【原创】论码农的财富修养
  5. Vue-cli 项目优化归纳(打包、源码、用户体验)
  6. myeclipse 添加mysql数据库_myeclipse添加数据库
  7. 模式识别中常见概率符号公式的学习笔记 By Youki~
  8. Dropout抑制过拟合
  9. lzg_ad:XPE中的EWF分区设置说明
  10. javaweb mysql毕业生管理系统_javaweb高校毕业生就业管理系统, springmvc+mysql
  11. 逻辑谬误_大规模分布式计算的谬误
  12. pngimg 可以商用吗_全球6大免费商用素材网!设计师必备!
  13. TiDB源码学习笔记:启动TiDB
  14. 我的Python心路历程 第十期 (10.5 股票实战之数据可视化曲线)
  15. 网络类型---P2P,MA
  16. 2022年了,云游戏离我们还有多远?【文末附彩蛋】
  17. rabbitMQ概述/在springboot下测试五种模式
  18. Android应用图标上的小红点Badge实现
  19. php采集所有a标签,dedecms采集去除a标签代码
  20. 盘古开天辟地之源码编译安装LAMP

热门文章

  1. oc总结第四讲:属性
  2. Shell脚本有什么用
  3. 如何理解关联法则中的三个判断准则
  4. 小姐姐用一周的时间,偷偷带你学Python,从小白到进阶,全站式保姆的Python基础教程导航帖(已完结)
  5. 大数据采集技术有哪些
  6. ESP32/ESP8266
  7. 古琴怎么学,初学者应该这么练(一)
  8. 如何统计代码总行数:指令
  9. 各种文件对应的文件类型
  10. 入职快手半年工作小结