base64编码
来源:互联网 发布:java怎么连接数据库 编辑:程序博客网 时间:2024/04/30 01:56
- /**
- * 对base64编码的封装
- *
- * @author
- */
- public class Base64Fiend {
- /**
- * 将byte类型的数据经过base64编码转为string.
- *
- * @param fileData
- * byte类型的数据 .
- * @return 转码后的数据,发生异常或者filedate为null时返回null.
- */
- public static String encode(byte[] fileData) {
- if (fileData == null) {
- //logger.info(("not get the fileData!"));
- return null;
- }
- BASE64Encoder encoder = new BASE64Encoder();
- return encoder.encode(fileData).replaceAll("//s*","");
- }
- /**
- * 将string类型的数据转码为byte类型.
- *
- * @param fileData
- * String 类型的数据.
- * @return 转码后的数据byte类型,发生异常或者filedate为null时返回null.
- */
- public static byte[] decode(String fileData) {
- if (fileData == null) {
- return null;
- }
- BASE64Decoder decoder = new BASE64Decoder();
- try {
- return decoder.decodeBuffer(fileData);
- } catch (IOException e) {
- e.printStackTrace();
- //logger.info(e.toString());
- }
- return null;
- }
- /**
- * 将InputStream类型的数据转码为String.
- *
- * @param fileData
- * InputStream类型的数据.
- * @return 转码后的数据String类型,发生异常或者filedate为null时返回null.
- */
- public static String encode(InputStream fileData) {
- if (fileData == null) {
- return null;
- }
- BASE64Encoder encoder = new BASE64Encoder();
- byte[] _fileData;
- try {
- _fileData = new byte[fileData.available()];
- fileData.read(_fileData);
- fileData.close();
- return encoder.encode(_fileData).replaceAll("//s*","");
- } catch (IOException e) {
- e.printStackTrace();
- //logger.info(e.toString());
- }
- return null;
- }
- /**对String编码.
- * @param string
- * @return
- */
- public static String encodeToString(String string){
- if(string==null){
- return null;
- }else{
- return encode(string.getBytes());
- }
- }
- public static String decodeToString(String string){
- if(string==null){
- return null;
- }else{
- byte[] _byte=decode(string);
- return new String(_byte);
- }
- }
- /**
- * 将对象编码为base64的String.
- *
- * @param object
- * 要进行编码的对象.
- * @return 编码后的对象对应的bease64String.
- */
- public static String encodeObject(Object object) {
- String objectString = null;
- ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
- try {
- ObjectOutputStream objectOutputStream = new ObjectOutputStream(
- arrayOutputStream);
- objectOutputStream.writeObject(object);
- objectString = encode(arrayOutputStream
- .toByteArray());
- } catch (IOException e) {
- //logger.info(e.toString());
- e.printStackTrace();
- }
- return objectString;
- }
- /**
- * 将bease64的String解编码为对象;
- *
- * @param objectString
- * 可以解编码为Object的Base64String,如果不能解编码为Object抛出异常.
- * @return 解编码成功后的对象.
- */
- public static Object decodeObject(String objectString) {
- Object object = null;
- ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
- decode(objectString));
- try {
- ObjectInputStream objectInputStream = new ObjectInputStream(
- byteArrayInputStream);
- object = objectInputStream.readObject();
- } catch (IOException e) {
- //logger.info(e.toString());
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- //logger.info(e.toString());
- e.printStackTrace();
- }
- return object;
- }
- }
- Base64编码
- Base64编码
- base64编码
- BASE64编码
- Base64编码
- BASE64编码
- base64 编码
- Base64编码
- Base64编码
- Base64编码
- base64编码
- BASE64编码
- base64编码
- BASE64编码
- base64编码
- base64编码
- base64编码
- base64 编码
- XSL的一些基础知识!
- GOOGLE 的疯狂面试题(转载)
- meta标签的用途!
- 沟通管理应用
- 单元测试中关于覆盖率的目标
- base64编码
- Linux系统安全工具之:Sxid和Skey
- 没有人比我更爱你
- 文件处理
- Mysql innodb行锁测试
- 如何保证数据同步!在WEB页面中!
- JSF整理(五)
- 软件测试的革命
- install mysql and gcc in linux as 4.5 use INSTALL-BINARY version