Base64了解(只是了解)

本文介绍了一种Base64编码和解码的方法,并提供了一个Java工具类实现。通过实例演示了如何使用该工具类对字符串进行编码和解码。

Base64了解

说明:

前几天学习碰到了base64,具体不知道什么意思

但是我根据网上的写法,写了一个工具类

用于base64对String字符串进行编码和解码

1.base64对string类型数据进行编码和解码

代码如下

@Test
public void test1() throws IOException {
	String content = "这是需要Base64编码的内容";
    // 创建一个Base64编码器
    BASE64Encoder base64Encoder = new BASE64Encoder();
    // 执行Base64编码操作
    String encodedString = base64Encoder.encode(content.getBytes("UTF-8"));

    System.out.println( encodedString );
    // 创建Base64解码器
    BASE64Decoder base64Decoder = new BASE64Decoder();
    // 解码操作
    byte[] bytes = base64Decoder.decodeBuffer(encodedString);

    String str = new String(bytes, "UTF-8");

    System.out.println(str);
}

说明:

在javaweb和maven的web项目中直接自带base64的编码解码方法

主要是

BASE64Encoder base64Encoder = new BASE64Encoder();

BASE64Decoder base64Decoder = new BASE64Decoder();

这两个类对类容进行编码和解码

2.自己封装成工具类

package com.shaoming.base64;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
base64编码和解码的工具类
*/
public class Base64Util {
	/**
	 * String content = "这是需要Base64编码的内容"; // 创建一个Base64编码器 BASE64Encoder
	 * base64Encoder = new BASE64Encoder(); // 执行Base64编码操作 String encodedString =
	 * base64Encoder.encode(content.getBytes("UTF-8"));
	 * 
	 * System.out.println( encodedString ); // 创建Base64解码器 BASE64Decoder
	 * base64Decoder = new BASE64Decoder(); // 解码操作 byte[] bytes =
	 * base64Decoder.decodeBuffer(encodedString);
	 * 
	 * String str = new String(bytes, "UTF-8");
	 * 
	 * System.out.println(str); }
	 * 
	 */
	private static BASE64Encoder base64Encoder = null;
	private static BASE64Decoder base64Decoder = null;
	static {
		base64Encoder = new BASE64Encoder();
		base64Decoder = new BASE64Decoder();
	}

	// 对字符窜进行base64编码
	public static String encode(String isEncodedString) {
		String encodeed = null;
		try {
			encodeed = base64Encoder.encode(isEncodedString.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("对string字符串进行base64编码失败");
		}
		return encodeed;

	}

	// 对字符窜进行base64进行解码
	public static String decode(String encodedString) {
		String encodedStringed = null;
		try {
			byte[] bytes = base64Decoder.decodeBuffer(encodedString);
			encodedStringed = new String(bytes, "UTF-8");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return encodedStringed;

	}
}

3.对工具类进行测试

@Test
public void test3() {
	System.out.println("需要进行base64编码的字符串内容如下:");
	String str = "Hello world 你好世界 !!!!";
	System.out.println(str);
	String encode = Base64Util.encode(str);
	System.out.println("对字符窜进行编码,内容如下");
	System.out.println(encode);
	String decode = Base64Util.decode(encode);
	System.out.println("对字符窜进行解码,内容如下");
	System.out.println(decode);
}

/**
控制台打印如下:
需要进行base64编码的字符串内容如下:
Hello world 你好世界 !!!!
对字符窜进行编码,内容如下
SGVsbG8gd29ybGQg5L2g5aW95LiW55WMICEhISE=
对字符窜进行解码,内容如下
Hello world 你好世界 !!!!

*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值