Chinaunix首页 | 论坛 | 博客
  • 博客访问: 289098
  • 博文数量: 88
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 840
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-20 21:13
文章分类

全部博文(88)

文章存档

2022年(1)

2017年(1)

2016年(2)

2015年(1)

2014年(83)

分类: Java

2016-03-27 21:36:39

详解微软 Nokia Lumia手机导出的短信如何导入到安卓手机中


最近换手机了,但是有个很坑爹的问题,那就是微软这货太与众不同了。

我先前使用的是wp的lumia手机,使用“传输我的数据”应用导入到sd卡后,短信备份文件居然是一个vmsg文件,找遍全网,没有看到一个现成的解决转换问题的方法。由此可见微软有多孤僻。。。

不过,还好我是程序猿,,攻城师,所以这个问题没有难倒我哦,现在是彻底解决了。

下面我详细说一下从前到后的所有过程
1. 首先要使用“传输我的数据”应用导出至sd卡,在应用中单击屏幕最下面的“...”就可以看到。
2.然后将sd卡的vmsg备份文件拷贝到电脑上

3.攻城师来了。。。
看代码:
总共有三个源代码:
代码中有个文件路径的字串,随自己的实际情况更改!!

(1)

点击(此处)折叠或打开

  1. import java.io.File;
  2. import java.nio.ByteBuffer;
  3. import java.nio.CharBuffer;
  4. import java.nio.charset.Charset;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Scanner;

  8. public class ConvertMain {

  9.     public static void main(String[] args) {
  10.         StringBuilder convertedString = new StringBuilder();
  11.         Map<String, Integer> map = new HashMap<String, Integer>();
  12.         int count = 1;
  13.         try {
  14.             Scanner scanner = new Scanner(new File("e:\\sms.vmsg"));
  15.             StringBuilder builder = new StringBuilder();
  16.             String str = new String();
  17.             Msg msg = new Msg();
  18.             do {
  19.                 str = scanner.nextLine();
  20.                 if (str.equals("BEGIN:VMSG")) {
  21.                     builder.append("sms,");
  22.                 } else if (str.startsWith("TEL")) {
  23.                     // 加入TEL
  24.                     msg.setTel(str.substring(4));
  25.                     if (!map.keySet().contains(str.substring(4))) {
  26.                         map.put(str.substring(4), count++);
  27.                     }

  28.                 } else if (str.startsWith("X-BOX")) {
  29.                     // 加入投送方式
  30.                     if (str.endsWith("INBOX")) {
  31.                         msg.setX_box(2);
  32.                     } else if (str.endsWith("SENDBOX")) {
  33.                         msg.setX_box(3);
  34.                     }
  35.                 } else if (str.startsWith("Date")) {
  36.                     // 加入日期
  37.                     msg.setDate(str.substring(5).replaceAll("/", "\\.")
  38.                             .substring(0, 16));
  39.                 } else if (str.startsWith("Subject")) {
  40.                     // 加入内容
  41.                     StringBuilder builder2 = new StringBuilder();
  42.                     builder2.append(str.substring(48));
  43.                     String str1 = scanner.nextLine();
  44.                     while (!str1.startsWith("END")) {
  45.                         builder2.append(str1);
  46.                         str1 = scanner.nextLine();
  47.                     }
  48.                     String source = builder2.toString().replaceAll("==", "=");
  49.                     byte[] array = getBytes(source.toCharArray());
  50.                     msg.setContent(QuotedPrintable.decode(array, "UTF-8")
  51.                             .replaceAll("\\s+", ""));
  52.                 }
  53.                 if (msg.getContent() != null && msg.getDate() != null
  54.                         && msg.getTel() != null && msg.getX_box() != 0) {
  55.                     if (msg.getX_box() == 2) {
  56.                         // deliver + ",count"
  57.                         builder.append("deliver,    " + msg.getTel() + ",,,"
  58.                                 + msg.getDate() + "," + map.get(msg.getTel())
  59.                                 + "," + msg.getContent() + "\r");
  60.                     } else if ((msg.getX_box() == 3)) {
  61.                         // send
  62.                         builder.append("submit,,    " + msg.getTel() + ",,"
  63.                                 + msg.getDate() + "," + map.get(msg.getTel())
  64.                                 + "," + msg.getContent() + "\r");
  65.                     }
  66.                     convertedString.append(builder.toString());
  67.                     msg = new Msg();
  68.                     builder = new StringBuilder();
  69.                 }
  70.             } while (str != "" && str != null);

  71.         } catch (Exception e) {
  72.         }
  73.         System.out.println(convertedString);
  74.     }

  75.     private static byte[] getBytes(char[] chars) {
  76.         Charset cs = Charset.forName("UTF-8");
  77.         CharBuffer cb = CharBuffer.allocate(chars.length);
  78.         cb.put(chars);
  79.         cb.flip();
  80.         ByteBuffer bb = cs.encode(cb);

  81.         return bb.array();

  82.     }
  83. /*
  84.     // byte转char
  85.     private static char[] getChars(byte[] bytes) {
  86.         Charset cs = Charset.forName("UTF-8");
  87.         ByteBuffer bb = ByteBuffer.allocate(bytes.length);
  88.         bb.put(bytes);
  89.         bb.flip();
  90.         CharBuffer cb = cs.decode(bb);

  91.         return cb.array();
  92.     }*/
  93. }
(2)

点击(此处)折叠或打开

  1. import java.io.UnsupportedEncodingException;


  2. public class QuotedPrintable {

  3.     private final static byte TAB = 0x09; // /t
  4.     private final static byte LF = 0x0A; // /n
  5.     private final static byte CR = 0x0D; // /r
  6.     //private final static byte SPACE = 0x20; // ' '
  7.     private final static byte EQUALS = 0x3D; // '='
  8.       
  9.     private final static byte LIT_START = 0x21;
  10.     private final static byte LIT_END = 0x7e;
  11.       
  12.     private final static int MAX_LINE_LENGTH = 76;
  13.       
  14.     private static int mCurrentLineLength = 0;
  15.   
  16.     /**
  17.      * A method to decode quoted printable encoded data.
  18.      * It overrides the same input byte array to save memory. Can be done
  19.      * because the result is surely smaller than the input.
  20.      *
  21.      * @param qp
  22.      * a byte array to decode.
  23.      * @return the length of the decoded array.
  24.      */
  25.     public static int decode(byte [] qp) {
  26.         int qplen = qp.length;
  27.         int retlen = 0;
  28.   
  29.         for (int i=0; i < qplen; i++) {
  30.             // Handle encoded chars
  31.             if (qp[i] == '=') {
  32.                 if (qplen - i > 2) {
  33.                     // The sequence can be complete, check it
  34.                     if (qp[i+1] == CR && qp[i+2] == LF) {
  35.                         // soft line break, ignore it
  36.                         i += 2;
  37.                         continue;
  38.   
  39.                     } else if (isHexDigit(qp[i+1]) && isHexDigit(qp[i+2]) ) {
  40.                         // convert the number into an integer, taking
  41.                         // the ascii digits stored in the array.
  42.                         qp[retlen++]=(byte)(getHexValue(qp[i+1])*16
  43.                                        + getHexValue(qp[i+2]));
  44.   
  45.                         i += 2;
  46.                         continue;
  47.   
  48.                     } else {
  49.                         System.out.println("decode: Invalid sequence = " + qp[i+1] + qp[i+2]);
  50.                     }
  51.                 }
  52.                 // In all wrong cases leave the original bytes
  53.                 // (see RFC 2045). They can be incomplete sequence,
  54.                 // or a '=' followed by non hex digit.
  55.             }
  56.   
  57.             // RFC 2045 says to exclude control characters mistakenly
  58.             // present (unencoded) in the encoded stream.
  59.             // As an exception, we keep unencoded tabs (0x09)
  60.             if( (qp[i] >= 0x20 && qp[i] <= 0x7f) ||
  61.                  qp[i] == TAB || qp[i] == CR || qp[i] == LF) {
  62.                 qp[retlen++] = qp[i];
  63.             }
  64.         }
  65.   
  66.         return retlen;
  67.     }
  68.   
  69.     private static boolean isHexDigit(byte b) {
  70.         return ( (b>=0x30 && b<=0x39) || (b>=0x41&&b<=0x46) );
  71.     }
  72.   
  73.     private static byte getHexValue(byte b) {
  74.         return (byte)Character.digit((char)b, 16);
  75.     }
  76.   
  77.     /**
  78.      *
  79.      * @param qp Byte array to decode
  80.      * @param enc The character encoding of the returned string
  81.      * @return The decoded string.
  82.      */
  83.     public static String decode(byte[] qp, String enc) {
  84.         int len=decode(qp);
  85.         try {
  86.             return new String(qp, 0, len, enc);
  87.         } catch (UnsupportedEncodingException e) {
  88.             return new String(qp, 0, len);
  89.         }
  90.     }
  91.   
  92.     /**
  93.      * A method to encode data in quoted printable
  94.      *
  95.      * @param content
  96.      * The string to be encoded
  97.      * @param enc
  98.      * The character encoding of the content string
  99.      * @return The encoded string. If the content is null, return null.
  100.      */
  101.     public static String encode(String content, String enc) {
  102.         if (content == null)
  103.             return null;
  104.           
  105.         byte[] str = null;
  106.         try {
  107.             str = content.getBytes(enc);
  108.         } catch (UnsupportedEncodingException e) {
  109.             str = content.getBytes();
  110.         }
  111.         return encode(str);
  112.     }
  113.     /**
  114.      * A method to encode data in quoted printable
  115.      *
  116.      * @param content
  117.      * The byte array of the string to be encoded
  118.      * @return The encoded string. If the content is null, return null.
  119.      */
  120.     public static String encode(byte[] content) {
  121.         if (content == null
阅读(1209) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~