青菜貓(孫宇博客),青菜貓(孫宇博客),青菜貓(孫宇博客)http://www.javasdc.cn/
          posts - 29,  comments - 63,  trackbacks - 0
             前幾天,客戶要求開發(fā)個(gè)小功能,就是把手機(jī)的VCF電話薄文件導(dǎo)出來后,可以導(dǎo)入數(shù)據(jù)庫,然后從數(shù)據(jù)庫出來的記錄,同樣可以生成vcf文件,并且地手機(jī)導(dǎo)入后可以成為有用電話薄.(vcf文件內(nèi)容格式另外有介紹)
             大家可以看看http://sourceforge.net/projects/mime-dir-j/這個(gè)開源的,前幾天還上不了個(gè)網(wǎng)站(我是用代理上的),這幾天好像是可以了,我是自己看了下源碼.然后寫了下,
            在java中生成vcf文件,我是在Servlet中用文件流做的部份代碼如下 :
           response.setHeader("Content-Disposition","attachment; filename="+ new String(bean.getTrueName().getBytes("GBK"), "ISO8859-1" )+".vcf");
                      try{
                          java.io.PrintWriter  bufout=response.getWriter();
                                    bufout.write("BEGIN:VCARD");
                                    bufout.write("\r\n");
                                    bufout.write("VERSION:2.1");
                                    bufout.write("\r\n");
                                    bufout.write("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:"+AddressServlet.qpEncodeing(bean.getTrueName())+";");
                                    bufout.write("\r\n");
                                    if(""!=bean.getMobile()&&bean.getMobile()!=null){
                                        bufout.write("TEL;CELL:"+bean.getMobile()+"");
                                        bufout.write("\r\n");
                                    }
                                    if(""!=bean.getWorkMobile()&&bean.getWorkMobile()!=null){
                                        bufout.write("TEL;WORK:"+bean.getWorkMobile()+"");
                                        bufout.write("\r\n");
                                    }
                                  
                                    if(""!=bean.getTelephone()&&bean.getTelephone()!=null){
                                        bufout.write("TEL;HOME:"+bean.getTelephone()+"");
                                        bufout.write("\r\n");
                                    }
                                    if(""!=bean.getEmail()&&bean.getEmail()!=null){
                                        bufout.write("EMAIL:"+bean.getEmail()+"");
                                        bufout.write("\r\n");
                                    }
                                    bufout.write("END:VCARD");
                                    bufout.write("\r\n");
                          response.getOutputStream().flush();
                          response.getOutputStream().close();
              }
                catch(IOException   E){
                  System.out.println("vcfexport發(fā)生I/O錯(cuò)誤!");
                      } 
          qpEncodeing()這個(gè)方法是專門編碼成這種格式的(http://www.aygfsteel.com/sundc/archive/2008/08/04/219863.html).

          2.從本地上傳后導(dǎo)入到DB,我也是在Servlet實(shí)現(xiàn)的
          FileItem fi=null;
                   try {
                          FileUpload fu = new FileUpload();
                          // 設(shè)置最大文件尺寸,這里是2MB
                          fu.setSizeMax(2097152);
                          // 得到所有的文件:
                          DiskFileItemFactory  factory = new DiskFileItemFactory();
                          fu.setFileItemFactory(factory);
                          List fileItems = fu.parseRequest(requestContext);
                          Iterator i = fileItems.iterator();
                          while(i.hasNext()) {
                              fi= (FileItem)i.next();
                              // 獲得文件名,這個(gè)文件名包括路徑:
                              String fileName = fi.getName();
                              if(fi.getSize()>2097152){
                                  return "文件太大";
                              }
                             
                              if(fileName==null){
                                  return "文件錯(cuò)誤";
                              }
                           String   fileExt=StringUtils.getFileExt(fileName).toLowerCase();
                              if (fileExt.toLowerCase().equals("vcf")){
                                 
                                   Properties p=this.createProperties();
                                      try {
                                          ArrayList li = this.importVCFFileContact(fi.getInputStream(), p);
                                          if(li==null||li.size()==0){
                                              return "導(dǎo)入記錄0條,可能是導(dǎo)入文件內(nèi)容格式不正確";
                                          }
                                          this.addAddresses(li);
                                          return "1";
                                      } catch (IOException e) {
                                          return "文件格式發(fā)生錯(cuò)誤";
                                      }   
                              }
                              else{
                                  return "不是vcf文件";
                              }
                             
                             
                          }
                      }
                      catch(Exception e) {
                           if(e.getMessage().indexOf("maximum")>0){
                               return "導(dǎo)入文件發(fā)生錯(cuò)誤,文件大于2M";
                              }
                         log.debug(e.getMessage());
                         return "導(dǎo)入文件發(fā)生錯(cuò)誤"+e.getMessage();
                      }
                     finally{
                         try{
                           //刪除臨時(shí)文件
                              fi.delete();
                         }
                         catch (Exception e) {
                          // TODO: handle exception
                      }
                       
                     }
                      return "發(fā)生錯(cuò)誤";
                   
                  }
          這個(gè)上傳中用到的代碼.下面是把VCF文件解析成bean對象
          /**
               * 導(dǎo)入聯(lián)系人
               * @param in
               * @throws SystemException
               */
              public ArrayList  importVCFFileContact(InputStream in,Properties pp) throws SystemException{
               
                       try {
                           BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                              Document document = new DocumentImpl();
                              BufferedWriter writer = null;
                           
                              String line;
                              StringBuffer bu=new StringBuffer();
                              while ((line = DecorGroup.nextLine(reader)) != 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());
                               while(m.find()){
                                   AddressBean  be=new AddressBean();
                                   be.setCateId(gb.getId());
                                   be.setUserId(gb.getUserId());
                                   String str=m.group(0);
                               
                          //姓名
                                   Pattern pn=Pattern.compile("N;([\\s\\S\\r\\n\\.]*?)([\\r\\n])");//分組,
                                   Matcher mn=pn.matcher(m.group(0));
                                   while(mn.find()){
                                       String name="";
                                     
                                      if(mn.group(1).indexOf("ENCODING=QUOTED-PRINTABLE")>-1){
                                          name=mn.group(1).substring(mn.group(1).indexOf("ENCODING=QUOTED-PRINTABLE:")+"ENCODING=QUOTED-PRINTABLE:".length());
                                          name=name.substring(name.indexOf(":")+1);
                                          if(name.indexOf(";")>-1){
                                              name=name.substring(0,name.indexOf(";"));
                                              be.setTrueName(AddressServlet.qpDecoding(name));
                                          }
                                          else{
                                              be.setTrueName(AddressServlet.qpDecoding(name));
                                          }
                                         
                                      }
                                      else{
                                          Pattern pnn=Pattern.compile("CHARSET=([A-Za-z0-9-]*?):");
                                          Matcher mnn=pnn.matcher(mn.group(1));
                                           while(mnn.find()){
                                               name=mn.group(1).substring(mn.group(1).indexOf(mnn.group(0))+mnn.group(0).length());
                                               be.setTrueName(name);
                                           }
                                      }
                                         
                                   }
                                   if(be.getTrueName().length()>20){
                                       return null;
                                   }
                                  String cell="";
                                  Pattern p1=Pattern.compile("TEL;CELL:\\d*");//分組,
                                  Matcher m1=p1.matcher(str);
                                  while(m1.find()){
                                      cell=m1.group(0).substring(m1.group(0).indexOf("TEL;CELL:")+"TEL;CELL:".length());
                                  }
                                  be.setMobile(cell);
                                   if(be.getMobile().length()>13){
                                       return null;
                                   }
                                  String work="";
                                  Pattern p2=Pattern.compile("TEL;WORK:\\d*");//分組,
                                  Matcher m2=p2.matcher(str);
                                  while(m2.find()){
                                      work=m2.group(0).substring(m2.group(0).indexOf("TEL;WORK:")+"TEL;WORK:".length());
                                  }
                                  be.setWorkMobile(work);
                                   if(be.getWorkMobile().length()>13){
                                       return null;
                                   }
                                  String home="";
                                  Pattern p3=Pattern.compile("TEL;HOME:\\d*");//分組,
                                  Matcher m3=p3.matcher(str);
                                  while(m3.find()){
                                  home=m3.group(0).substring(m3.group(0).indexOf("TEL;HOME:")+"TEL;HOME:".length());
                                  }
                                  be.setTelephone(home);
                                  if(be.getTelephone().length()>13){
                                       return null;
                                   }

                                  String email="";
                                  Pattern p4=Pattern.compile("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+");//分組,
                                  Matcher m4=p4.matcher(str);
                                  while(m4.find()){
                                      email=m4.group(0);
                                  }
                                  be.setEmail(email);
                                
                               }
                               reader.close();
                      } catch (Exception e) {
                          log.debug(e.getMessage());
                          throw new SystemException("對不起,系統(tǒng)忙,請梢后再試!");
                      }
                  return abs;
                 
              }
          以下是源碼中自代的方法,我COPY過來就用了
           public static String nextLine(BufferedReader reader) throws IOException {
                 
                      String line;
                      String nextLine;
                     
                      do {
                          line = reader.readLine();
                          if (line == null) return null;
                      } while (line.length() == 0);
                     
                      // Evolution style line folding
                      while (line.endsWith("=")) {
                          line = line.substring(0, line.length() - 1);
                          line += reader.readLine();
                          }
                      // RFC 2425 line folding
                      reader.mark(1000);
                      nextLine = reader.readLine();
                      if ((nextLine != null)
                          && (nextLine.length() > 0)
                          && ((nextLine.charAt(0) == 0x20) // white space
                              || (nextLine.charAt(0) == 0x09))) { // tab
                          line += nextLine.substring(1);
                      } else {
                          reader.reset();
                      }
                     
                      line = line.trim();
                      return line;
                  }
               在這里要提醒大家,有的手機(jī)的電話薄可能字選項(xiàng)會多一點(diǎn).所以,我在這做的是  姓名   手機(jī)號碼 電話  工作電話   Email這幾個(gè)大家手機(jī)都會有的. 程序通過諾基亞,三星(SAMSUNG),摩托羅拉(MOTO),還有國產(chǎn)波導(dǎo),哈哈.....

              如有問題,大家可以評論...
          posted on 2008-08-04 11:50 青菜貓(孫宇) 閱讀(5696) 評論(0)  編輯  收藏 所屬分類: java
          <2008年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          青菜貓(孫宇)結(jié)交天下朋友,在網(wǎng)上吸取知識..

          常用鏈接

          留言簿(16)

          隨筆分類

          隨筆檔案

          文章分類

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          青菜貓(孫宇博客),青菜貓(孫宇博客),青菜貓(孫宇博客)http://www.javasdc.cn/
          主站蜘蛛池模板: 织金县| 江川县| 彩票| 长治县| 察雅县| 特克斯县| 舞阳县| 贡嘎县| 巨鹿县| 大庆市| 济宁市| 黔江区| 武定县| 新津县| 泸定县| 大新县| 石渠县| 利川市| 长垣县| 固镇县| 绥化市| 靖远县| 潞西市| 阜阳市| 皮山县| 钟山县| 锦州市| 太白县| 齐齐哈尔市| 昭苏县| 凤阳县| 南郑县| 鄂尔多斯市| 罗城| 安泽县| 济源市| 万宁市| 岳阳市| 钦州市| 项城市| 天等县|