图片数据Base64编解码(java实现)

package com.tianmushanlu.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * 参考至博客 http://www.cnblogs.com/phinecos/archive/2008/09/23/1297292.html
 * 
 *
 */
public class Base64Util {
	
	/**
	 * 测试main方法
	 * @param args
	 */
	public static void main(String[] args) {
		String imageStr = getImageStr("D:\\image.jpg");
		System.out.println(imageStr);
		generateImage(imageStr,"generateImage.jpg");
	}
	
	private static String LOCAL_FILE_PATH;
	static {
		LOCAL_FILE_PATH = System.getProperty("user.home");
		// 如果不是以文件分隔符结尾,就补充文件分隔符
		if (!LOCAL_FILE_PATH.endsWith(File.separator)) {
			LOCAL_FILE_PATH += File.separator;
		}
		LOCAL_FILE_PATH += "conf" + File.separator + "image" + File.separator;
	}

	/**
	 * 生成字节数组字符串
	 * 
	 * @param imgBase64Str
	 *            经过Base64编码过的字节数组字符串
	 * @return 字节数组字符串
	 */
	public static byte[] generateByte(String imgBase64Str) {
		byte[] b = new byte[3072];
		if (imgBase64Str == null) {
			return b;
		}

		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			b = decoder.decodeBuffer(imgBase64Str);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			return b;
		} catch (Exception e) {
			return b;
		}
	}

	/**
	 * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
	 * 
	 * @param imgContent
	 *            待处理的图片
	 * @return 返回Base64编码过的字节数组字符串
	 */
	public static String getImageStr(String imgContent) {
		String imgBase64Str = null;
		InputStream in = null;
		byte[] data = null;
		if( imgContent == null || ("").equals(imgContent) ) {
			return imgBase64Str;
		}
		// 读取图片字节数组
		try {
			in = new FileInputStream(imgContent);
			data = new byte[in.available()];
			in.read(data);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 对字节数组Base64编码
		BASE64Encoder encoder = new BASE64Encoder();
		imgBase64Str = encoder.encode(data);
		return imgBase64Str;
	}

	/**
	 * 对字节数组字符串进行Base64解码并生成图片
	 * 
	 * @param imgBase64Str
	 *            经过Base64编码过的字节数组字符串
	 * @param imgName
	 *            图片名
	 * @return 是否生成图片
	 */
	public static boolean generateImage(String imgBase64Str, String imgName) {
		OutputStream out = null;
		if (imgName == null || ("").equals(imgName)) {
			return false;
		}
		
		if (imgBase64Str == null || ("").equals(imgBase64Str)) {
			return false;
		}
			
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(imgBase64Str);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			/**
			 * 生成图片文件,存储路径为C:\Users\xxx(你的主机名)\conf\temp
			 *
			 */
			//如果目录不存在,创建目录
			File localImageFolder = new File(LOCAL_FILE_PATH);
			if(!localImageFolder.exists()) {
				localImageFolder.mkdirs();
			}
			//如果目录不为空清空目录,因为都是单线程操作,这里清除不会相互影响
			File[] subFiles = localImageFolder.listFiles();
			if(null != subFiles) {
				for(File subFile : subFiles) {
					subFile.delete();
				}
			}
			
			out = new FileOutputStream(LOCAL_FILE_PATH + imgName);
			out.write(b);
			out.flush();			
			return true;
		} catch (Exception e) {
			return false;
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

猜你喜欢

转载自tianmushanlu.iteye.com/blog/2305955