java vcf_Java VCF 格式解析

Java VCF 格式解析

vcf可以用记事本打开,格式如下:

BEGIN:VCARD

VERSION:2.1

N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=BC=A0=E4=B8=89;;

FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=BC=A0=E4=B8=89

TEL;VOICE;PREF:1-370-000-0000

PHOTO;ENCODING=BASE64;PNG:iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAIAAABt+uBvAAAA。。。。

END:VCARD

java code:

import java.awt.image.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class VCFTest {

public static void main(String[] args) {

String name = "张三";

VCFTest vt =new VCFTest();

try {

//将字符串转换成utf-8 quoted-printable 格式

String str = vt.qpEncodeingUTF8(name);

System.out.println(str);

//将字符串utf-8 quoted-printable 格式 转换成正常格式

str = vt.qpDecodingUTF8(str);

System.out.println(str);

String path ="d:\\aa.png";

//将图片转换成base64格式字符

//str = vt.BASE64Encodeing(path);

//将base64格式字符转换成图片

//vt.BASE64Decoding(str,"png", path);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

* 编码 图片 转换为 BASE64

* @param path 图片路径

* @return 转换后的字符串

* @throws Exception

*/

public String BASE64Encodeing(String path) throws Exception {

BASE64Encoder encoder = new BASE64Encoder();

File file = new File(path);

FileInputStream fis = new FileInputStream(file);

byte[] buffer = new byte[(int) file.length()];

fis.read(buffer);

return encoder.encode(buffer);

}

/**

* 解码 BASE64 保存为图片

* @param str 需要解码的字符串

* @param imgType 解码图片类型

* @param path 解码后图片保存的位置

* @return 解码是否完成

* @throws Exception

*/

public boolean BASE64Decoding(String str, String imgType, String path) throws Exception {

BASE64Decoder decoder = new BASE64Decoder();

byte[] ib = decoder.decodeBuffer(str);

BufferedImage bi = ImageIO.read(new ByteArrayInputStream(ib));

boolean isFinish = ImageIO.write(bi, imgType, new File(path));

bi.flush();

return isFinish;

}

/**

* 编码 UTF8 quoted-printable

* @param str 需要编码的字符串

* @return 编码后的字符串

* @throws Exception

*/

public String qpEncodeingUTF8(String str) throws Exception {

if (str != null) {

char[] encode = str.toCharArray();

StringBuffer sb = new StringBuffer();

for (int i = 0; i < encode.length; i++) {

if ((encode[i] >= '!') && (encode[i] <= '~')

&& (encode[i] != '=') && (encode[i] != '\n')) {

sb.append(encode[i]);

} else if (encode[i] == '=') {

sb.append("=3D");

} else if (encode[i] == '\n') {

sb.append("\n");

} else {

StringBuffer sbother = new StringBuffer();

sbother.append(encode[i]);

String ss = sbother.toString();

byte[] buf = null;

buf = ss.getBytes("utf-8");

// UTF-8: buf.length == 3

// GBK: buf.length == 2

if (buf.length == 3) {

for (int j = 0; j < buf.length; j++) {

String s16 = String.valueOf(Integer

.toHexString(buf[j]));

char c16_6;

char c16_7;

if (s16.charAt(6) >= 97 && s16.charAt(6) <= 122) {

c16_6 = (char) (s16.charAt(6) - 32);

} else {

c16_6 = s16.charAt(6);

}

if (s16.charAt(7) >= 97 && s16.charAt(7) <= 122) {

c16_7 = (char) (s16.charAt(7) - 32);

} else {

c16_7 = s16.charAt(7);

}

sb.append("=" + c16_6 + c16_7);

}

}

}

}

str = sb.toString();

}

return str;

}

/**

* 解码 UTF8 quoted-printable

* @param str 需要解码的字符串

* @return 解码后的字符串

* @throws Exception

*/

public String qpDecodingUTF8(String str) throws Exception {

if (str != null) {

StringBuffer sb = new StringBuffer(str);

for (int i = 0; i < sb.length(); i++) {

if (sb.charAt(i) == '\n' && sb.charAt(i - 1) == '=') {

sb.deleteCharAt(i - 1);

}

}

str = sb.toString();

byte[] bytes = str.getBytes("US-ASCII");

for (int i = 0; i < bytes.length; i++) {

byte b = bytes[i];

if (b != 95) {

bytes[i] = b;

} else {

bytes[i] = 32;

}

}

if (bytes != null) {

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

for (int i = 0; i < bytes.length; i++) {

int b = bytes[i];

if (b == '=') {

try {

int u = Character.digit((char) bytes[++i], 16);

int l = Character.digit((char) bytes[++i], 16);

if (u == -1 || l == -1) {

continue;

}

buffer.write((char) ((u << 4) + l));

} catch (ArrayIndexOutOfBoundsException e) {

e.printStackTrace();

}

} else {

buffer.write(b);

}

}

str = new String(buffer.toByteArray(), "UTF-8");

}

}

return str;

}

}

已有 0 人发表留言,猛击->> 这里<

ITeye推荐

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值