处理 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();
    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值