处理 vcard

2023-12-05 05:48
文章标签 处理 vcard

本文主要是介绍处理 vcard,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1)导入

package com.ad.web.servlet.common;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import com.ad.web.rmapi.request.AddUerReq;

/**
 * 读取导入的vcard人员信息,保存到内存当中
 *
 * @author zxkj
 *
 */
public class ImpAddrVCF
{

    private static final Logger log = Logger.getLogger(ImpAddrVCF.class);

    public static List<AddUerReq> ProcessVCFString(String path)
            throws IOException
    {

        List<AddUerReq> list = new ArrayList<AddUerReq>();
        BufferedReader reader = null;
        try
        {
            String code = EncodingDetect.getJavaEncode(path);
            reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(path), code));

            String line;
            StringBuffer bu = new StringBuffer();
            while ((line = reader.readLine()) != null)
            {
                bu.append(line + "\r\n");
            }
            Pattern p = Pattern
                    .compile("BEGIN:VCARD(\\r\\n)([\\s\\S\\r\\n\\.]*?)END:VCARD");// 分组,
            Matcher m = p.matcher(bu.toString());

            int i = 0;
            while (m.find())
            {
                AddUerReq user = new AddUerReq();
                user.setMarkid(i);
                i++;

                // 姓名
                Pattern pn = Pattern
                        .compile("FN:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mn = pn.matcher(m.group(0));
                while (mn.find())
                {
                    String temp = mn.group();

                    String name = temp.substring(temp.indexOf("FN:") + 3, temp
                            .length());

                    if(name!=null && !"".equals(name))
                    {
                        
                        user.setName(Check.FileuploadCheckchart(name));
                    }
                    else
                    {
                        user.setName("");
                    }
                    

                }

                // 常用邮箱
                Pattern pemail = Pattern
                        .compile("EMAIL;TYPE=INTERNET;TYPE=pref:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher memial = pemail.matcher(m.group(0));
                while (memial.find())
                {

                    String temp = memial.group();
                    String email = temp.substring(temp.indexOf("EMAIL;TYPE=INTERNET;TYPE=pref:") + 30,
                            temp.length());
                    if(email!=null && !"".equals(email))
                    {
                        
                        user.setEmail(Check.FileuploadCheckchart(email));
                    }
                    else
                    {
                        user.setEmail("");
                    }
                    
                    

                }

                // 备用邮箱1
                Pattern pemail1 = Pattern
                        .compile("EMAIL;RESERVE1:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher memial1 = pemail1.matcher(m.group(0));
                while (memial1.find())
                {
                    String temp = memial1.group();
                    String email1 = temp.substring(
                            temp.indexOf("EMAIL;RESERVE1:") + 15, temp.length());
                    
                    if(email1!=null && !"".equals(email1))
                    {
                        
                        user.setEmail1(Check.FileuploadCheckchart(email1));

                    }
                    else
                    {
                        user.setEmail1("");

                    }
                    
                    
                }

                // 备用邮箱2
                Pattern pemail2 = Pattern
                        .compile("EMAIL;RESERVE2:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher memial2 = pemail2.matcher(m.group(0));
                while (memial2.find())
                {
                    String temp = memial2.group();
                    String email2 = temp.substring(
                            temp.indexOf("EMAIL;RESERVE2:") + 15, temp.length());
                    if(email2!=null && !"".equals(email2))
                    {
                        
                        user.setEmail2(Check.FileuploadCheckchart(email2));
                    }
                    else
                    {
                        user.setEmail2("");
                    }
                    
                }

                // 常用电话
                Pattern pmobile = Pattern
                        .compile("TEL;CELL;VOICE:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mmobile = pmobile.matcher(m.group(0));
                while (mmobile.find())
                {
                    String temp = mmobile.group();
                    String mobile = temp.substring(temp.indexOf("TEL;CELL;VOICE:") + 15,
                            temp.length());
                    
                    if(mobile!=null && !"".equals(mobile))
                    {
                        
                        user.setPhone(Check.FileuploadCheckchart(mobile));
                    }
                    else
                    {
                        user.setPhone("");
                    }
                    
                }

                // 备用电话1
                Pattern pmobile1 = Pattern
                        .compile("TEL;RESERVE1:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mmobile1 = pmobile1.matcher(m.group(0));
                while (mmobile1.find())
                {
                    String temp = mmobile1.group();
                    String mobile = temp.substring(
                            temp.indexOf("TEL;RESERVE1:") + 13, temp.length());
                    
                    if(mobile!=null && !"".equals(mobile))
                    {
                        
                        user.setPhone1(Check.FileuploadCheckchart(mobile));
                    }
                    else
                    {
                        user.setPhone1("");
                    }
                    
                }

                // 备用电话2
                Pattern pmobile2 = Pattern
                        .compile("TEL;RESERVE2:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mmobile2 = pmobile2.matcher(m.group(0));
                while (mmobile2.find())
                {
                    String temp = mmobile2.group();
                    String mobile2 = temp.substring(
                            temp.indexOf("TEL;RESERVE2:") + 13, temp.length());
                    
                    if(mobile2!=null && !"".equals(mobile2))
                    {
                        
                        user.setPhone2(Check.FileuploadCheckchart(mobile2));
                    }
                    else
                    {
                        user.setPhone2("");
                    }
                    
                }

                // 固定电话
                Pattern ptelephone = Pattern
                        .compile("TELEPHONE:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mtelephone = ptelephone.matcher(m.group(0));
                while (mtelephone.find())
                {
                    String temp = mtelephone.group();
                    String telephone = temp.substring(temp
                            .indexOf("TELEPHONE:") + 10, temp.length());
                    if(telephone!=null && !"".equals(telephone))
                    {
                        
                        user.setTelephone(Check.FileuploadCheckchart(telephone));

                    }
                    else
                    {
                        user.setTelephone("");

                    }
                    
                    
                }

                // 生日
                Pattern pbirthday = Pattern
                        .compile("BIRTHDAY:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mbirthday = pbirthday.matcher(m.group(0));
                while (mbirthday.find())
                {
                    String temp = mbirthday.group();
                    String birthday = temp.substring(
                            temp.indexOf("BIRTHDAY:") + 9, temp.length());
                    if(birthday!=null && !"".equals(birthday))
                    {
                        
                        user.setBirthday(Check.FileuploadCheckchart(birthday));
                    }
                    else
                    {
                        user.setBirthday("");
                    }
                    

                }

                // 家庭住址
                Pattern paddress = Pattern
                        .compile("ADDRESS:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher maddress = paddress.matcher(m.group(0));
                while (maddress.find())
                {
                    String temp = maddress.group();
                    String address = temp.substring(
                            temp.indexOf("ADDRESS:") + 8, temp.length());
                    
                    if(address!=null && !"".equals(address))
                    {
                        
                        user.setHome_address(Check.FileuploadCheckchart(address));
                    }
                    else
                    {
                        user.setHome_address("");
                    }
                    
                    
                }

                // 公司
                Pattern pcompany = Pattern
                        .compile("COMPANY:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mcompany = pcompany.matcher(m.group(0));
                while (mcompany.find())
                {
                    String temp = mcompany.group();
                    String company = temp.substring(
                            temp.indexOf("COMPANY:") + 8, temp.length());
                    
                    if(company!=null && !"".equals(company))
                    {
                        
                        user.setCompany(Check.FileuploadCheckchart(company));
                    }
                    else
                    {
                        user.setCompany("");
                    }
                    
                }

                // 备注
                Pattern pmomo = Pattern
                        .compile("MOMO:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mmomo = pmomo.matcher(m.group(0));
                while (mmomo.find())
                {
                    String temp = mmomo.group();
                    String momo = temp.substring(temp.indexOf("MOMO:") + 5,
                            temp.length());
                    
                    if(momo!=null && !"".equals(momo))
                    {
                        
                        user.setRemark(Check.FileuploadCheckchart(momo));
                    }
                    else
                    {
                        user.setRemark("");
                    }
                    

                }
                // 所属分组
                Pattern pgroup = Pattern
                        .compile("GROUPS:([\\s\\S\\r\\n\\.]*?)([\\r\\n])");// 分组,
                Matcher mgroup = pgroup.matcher(m.group(0));
                while (mgroup.find())
                {
                    String temp = mgroup.group();
                    String group = temp.substring(temp.indexOf("GROUPS:") + 7,
                            temp.length());

                    if(group!=null && !"".equals(group))
                    {
                        
                        user.setGroup(Check.FileuploadCheckchart(group));
                    }
                    else
                    {
                        user.setGroup("");
                    }
                    
                    

                }

                list.add(user);
            }
        }
        catch (Exception e)
        {
            log.error("imp vcf is error:", e);
            return list;
        }
        finally
        {
            if (reader != null)
            {
                reader.close();
            }
        }

        return list;
    }

    /**
     * 对导入的vcard格式进行校验
     *
     * @param path
     * @return
     * @throws IOException
     */
    public static boolean checkvcfHead(String path) throws IOException
    {
        boolean flag = false;
        try
        {
            BufferedReader reader;
            reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(path), "utf-8"));
            String line;
            StringBuffer bu = new StringBuffer();
            while ((line = reader.readLine()) != null)
            {
                bu.append(line + "\r\n");
            }
            Pattern p = Pattern
                    .compile("BEGIN:VCARD(\\r\\n)([\\s\\S\\r\\n\\.]*?)END:VCARD");// 分组,
            Matcher m = p.matcher(bu.toString());
            flag = m.find();
        }
        catch (Exception e)
        {
            log.error("checkvcfHead is error:", e);
            return flag;
        }

        return flag;
    }

}

二)导出

package com.ad.web.servlet.common;

import java.util.ArrayList;
import java.util.List;

import com.ad.web.rmapi.pojo.ExpUser;

/**
 * bean组装cvs格式
 *
 * @author zxkj
 *
 */
public class ExpAddrVCF
{

    public static String getVCFString(List<ExpUser> lists)
    {
        StringBuffer sbf = new StringBuffer();
        
        if(lists!=null)
        {
            for (int i = 0; i < lists.size(); i++)
            {
                ExpUser user = lists.get(i);
                sbf.append("BEGIN:VCARD");
                sbf.append("\r\n");
                sbf.append("VERSION:3.0");
                sbf.append("\r\n");
                String name=user.getFirst_name();
                
                if(name!=null && !"".equals(name))
                {
                    sbf.append("FN:" + Check.FileuploadCheckchart(name));
                }
                else
                {
                    sbf.append("FN:" + "");
                }
                sbf.append("\r\n");
                String first=user.getFirst_name();
                
                if(name!=null && !"".equals(name))
                {
                    sbf.append("N:" +Check.FileuploadCheckchart(first));
                }
                else
                {
                    sbf.append("FN:" + "");
                }
                
                //sbf.append("N:" + first);
                sbf.append("\r\n");
                String[] emails = user.getEmail();
                List<String> emailList=new ArrayList<String>();
                if(emails!=null)
                {
                    for(int e=0;e<emails.length;e++)
                    {
                        
                        emailList.add(Check.FileuploadCheckchart(emails[e]));
                    }
                    int temp=3-emailList.size();
                    if(temp>0)
                    {
                        for(int p=0;p<temp;p++)
                        {
                            emailList.add("");
                        }
                    }
                    for (int j = 0;j < 3; j++)
                    {
                        if (j == 0)
                        {
                            String email=emailList.get(j);
                            
                            if(email!=null && !"".equals(email))
                            {
                                sbf.append("EMAIL;TYPE=INTERNET;TYPE=pref:" +Check.FileuploadCheckchart(email));
                            }
                            else
                            {
                                sbf.append("EMAIL;TYPE=INTERNET;TYPE=pref:" +"");
                            }
                            sbf.append("\r\n");
                        }
                        if (j == 1)
                        {
                            String reserve1=emailList.get(j);
                            
                            if(reserve1!=null && !"".equals(reserve1))
                            {
                                sbf.append("EMAIL;RESERVE1:" +Check.FileuploadCheckchart(reserve1));
                            }
                            else
                            {
                                sbf.append("EMAIL;RESERVE1:" + "");
                            }
                            
                            sbf.append("\r\n");
                        }
                        if (j == 2)
                        {
                            String reserve2=emailList.get(j);
                            
                            if(reserve2!=null && !"".equals(reserve2))
                            {
                                sbf.append("EMAIL;RESERVE2:" +Check.FileuploadCheckchart(reserve2));
                            }
                            else
                            {
                                sbf.append("EMAIL;RESERVE2:" + "");
                            }
                            
                            sbf.append("\r\n");
                        }
                    }
                }
                
                List<String> mobiles = user.getMobile_number();
                List<String> mobileList=new ArrayList<String>();
                if(mobiles!=null)
                {
                    for(int e=0;e<mobiles.size();e++)
                    {
                        mobileList.add(mobiles.get(e));
                    }
                    int t_mobile=3-mobileList.size();
                    if(t_mobile>0)
                    {
                        for(int p=0;p<t_mobile;p++)
                        {
                            mobileList.add("");
                        }
                    }
                    for (int m = 0;m < 3; m++)
                    {
                        if (m == 0)
                        {
                            String cell=mobileList.get(m);
                            
                            if(cell!=null && !"".equals(cell))
                            {
                                sbf.append("TEL;CELL;VOICE:" +Check.FileuploadCheckchart(cell));
                            }
                            else
                            {
                                sbf.append("TEL;CELL;VOICE:" + "");
                            }
                            
                            sbf.append("\r\n");
                        }
                        if (m == 1)
                        {
                            String cell1=mobileList.get(m);
                            
                            if(cell1!=null && !"".equals(cell1))
                            {
                                sbf.append("TEL;RESERVE1:" +Check.FileuploadCheckchart(cell1));
                            }
                            else
                            {
                                sbf.append("TEL;RESERVE1:" + "");
                            }
                            
                        
                            sbf.append("\r\n");
                        }
                        if (m == 2)
                        {
                            String cell2=mobileList.get(m);
                            
                            if(cell2!=null && !"".equals(cell2))
                            {
                                sbf.append("TEL;RESERVE2:" +Check.FileuploadCheckchart(cell2));
                            }
                            else
                            {
                                sbf.append("TEL;RESERVE2:" + "");
                            }
                            sbf.append("\r\n");
                        }
                    }
                }
                String  telephone=user.getTelephone();
                
                if(telephone!=null && !"".equals(telephone))
                {
                    sbf.append("TELEPHONE:" +Check.FileuploadCheckchart(telephone));
                }
                else
                {
                    sbf.append("TELEPHONE:" + "");
                }
                sbf.append("\r\n");
                
                String  birthday=user.getBirthday();
                
                if(birthday!=null && !"".equals(birthday))
                {
                    sbf.append("BIRTHDAY:" +Check.FileuploadCheckchart(birthday));
                }
                else
                {
                    sbf.append("BIRTHDAY:" +"");
                }
                sbf.append("\r\n");
                String  address=user.getHome_address();
                
                if(address!=null && !"".equals(address))
                {
                    sbf.append("ADDRESS:" +Check.FileuploadCheckchart(address));
                }
                else
                {
                    sbf.append("ADDRESS:" + "");
                }
                
                sbf.append("\r\n");
                String  company=user.getCompany_name();
                
                if(company!=null && !"".equals(company))
                {
                    sbf.append("COMPANY:" +Check.FileuploadCheckchart(company));
                }
                else
                {
                    sbf.append("COMPANY:" + "");
                }
                sbf.append("\r\n");
                String  momo=user.getMemo();
                
                if(momo!=null && !"".equals(momo))
                {
                    sbf.append("MOMO:" +Check.FileuploadCheckchart(momo));
                }
                else
                {
                    sbf.append("MOMO:" + "");
                }
                sbf.append("\r\n");
                String  groups=user.getGroupName();
                
                if(groups!=null && !"".equals(groups))
                {
                    sbf.append("GROUPS:" +Check.FileuploadCheckchart(groups));
                }
                else
                {
                    sbf.append("GROUPS:" + "");
                }
                
                sbf.append("\r\n");
                sbf.append("END:VCARD");
                sbf.append("\r\n");
            }
        }
        

        return sbf.toString();
    }

}



这篇关于处理 vcard的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/456395

相关文章

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr

requests处理token鉴权接口和jsonpath使用方式

《requests处理token鉴权接口和jsonpath使用方式》文章介绍了如何使用requests库进行token鉴权接口的处理,包括登录提取token并保存,还详述了如何使用jsonpath表达... 目录requests处理token鉴权接口和jsonpath使用json数据提取工具总结reques

C# 空值处理运算符??、?. 及其它常用符号

《C#空值处理运算符??、?.及其它常用符号》本文主要介绍了C#空值处理运算符??、?.及其它常用符号,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、核心运算符:直接解决空值问题1.??空合并运算符2.?.空条件运算符二、辅助运算符:扩展空值处理

浅析Python中如何处理Socket超时

《浅析Python中如何处理Socket超时》在网络编程中,Socket是实现网络通信的基础,本文将深入探讨Python中如何处理Socket超时,并提供完整的代码示例和最佳实践,希望对大家有所帮助... 目录开篇引言核心要点逐一深入讲解每个要点1. 设置Socket超时2. 处理超时异常3. 使用sele

SpringMVC配置、映射与参数处理​入门案例详解

《SpringMVC配置、映射与参数处理​入门案例详解》文章介绍了SpringMVC框架的基本概念和使用方法,包括如何配置和编写Controller、设置请求映射规则、使用RestFul风格、获取请求... 目录1.SpringMVC概述2.入门案例①导入相关依赖②配置web.XML③配置SpringMVC

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

5 种使用Python自动化处理PDF的实用方法介绍

《5种使用Python自动化处理PDF的实用方法介绍》自动化处理PDF文件已成为减少重复工作、提升工作效率的重要手段,本文将介绍五种实用方法,从内置工具到专业库,帮助你在Python中实现PDF任务... 目录使用内置库(os、subprocess)调用外部工具使用 PyPDF2 进行基本 PDF 操作使用

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺