处理 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

相关文章

电脑提示xlstat4.dll丢失怎么修复? xlstat4.dll文件丢失处理办法

《电脑提示xlstat4.dll丢失怎么修复?xlstat4.dll文件丢失处理办法》长时间使用电脑,大家多少都会遇到类似dll文件丢失的情况,不过,解决这一问题其实并不复杂,下面我们就来看看xls... 在Windows操作系统中,xlstat4.dll是一个重要的动态链接库文件,通常用于支持各种应用程序

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

Golang 日志处理和正则处理的操作方法

《Golang日志处理和正则处理的操作方法》:本文主要介绍Golang日志处理和正则处理的操作方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录1、logx日志处理1.1、logx简介1.2、日志初始化与配置1.3、常用方法1.4、配合defer

springboot加载不到nacos配置中心的配置问题处理

《springboot加载不到nacos配置中心的配置问题处理》:本文主要介绍springboot加载不到nacos配置中心的配置问题处理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录springboot加载不到nacos配置中心的配置两种可能Spring Boot 版本Nacos

python web 开发之Flask中间件与请求处理钩子的最佳实践

《pythonweb开发之Flask中间件与请求处理钩子的最佳实践》Flask作为轻量级Web框架,提供了灵活的请求处理机制,中间件和请求钩子允许开发者在请求处理的不同阶段插入自定义逻辑,实现诸如... 目录Flask中间件与请求处理钩子完全指南1. 引言2. 请求处理生命周期概述3. 请求钩子详解3.1

Python处理大量Excel文件的十个技巧分享

《Python处理大量Excel文件的十个技巧分享》每天被大量Excel文件折磨的你看过来!这是一份Python程序员整理的实用技巧,不说废话,直接上干货,文章通过代码示例讲解的非常详细,需要的朋友可... 目录一、批量读取多个Excel文件二、选择性读取工作表和列三、自动调整格式和样式四、智能数据清洗五、

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失