Base64工具类合集Base64Utils

这个Java类库提供了一套简洁的Base64编码和解码实用方法,适配了Java 8的`java.util.Base64`。主要功能包括标准Base64编码和解码,以及URL和文件名安全的Base64编码和解码。此外,还提供了将编码后的字节数组转换为字符串和从UTF-8字符串解码的方法。

直接上代码!

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * A simple utility class for Base64 encoding and decoding.
 *
 * <p>Adapts to Java 8's {@link java.util.Base64} in a convenience fashion.
 *
 * @author Juergen Hoeller
 * @author Gary Russell
 * @since 4.1
 * @see java.util.Base64
 */
public abstract class Base64Utils {

	private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;


	/**
	 * Base64-encode the given byte array.
	 * @param src the original byte array
	 * @return the encoded byte array
	 */
	public static byte[] encode(byte[] src) {
		if (src.length == 0) {
			return src;
		}
		return Base64.getEncoder().encode(src);
	}

	/**
	 * Base64-decode the given byte array.
	 * @param src the encoded byte array
	 * @return the original byte array
	 */
	public static byte[] decode(byte[] src) {
		if (src.length == 0) {
			return src;
		}
		return Base64.getDecoder().decode(src);
	}

	/**
	 * Base64-encode the given byte array using the RFC 4648
	 * "URL and Filename Safe Alphabet".
	 * @param src the original byte array
	 * @return the encoded byte array
	 * @since 4.2.4
	 */
	public static byte[] encodeUrlSafe(byte[] src) {
		if (src.length == 0) {
			return src;
		}
		return Base64.getUrlEncoder().encode(src);
	}

	/**
	 * Base64-decode the given byte array using the RFC 4648
	 * "URL and Filename Safe Alphabet".
	 * @param src the encoded byte array
	 * @return the original byte array
	 * @since 4.2.4
	 */
	public static byte[] decodeUrlSafe(byte[] src) {
		if (src.length == 0) {
			return src;
		}
		return Base64.getUrlDecoder().decode(src);
	}

	/**
	 * Base64-encode the given byte array to a String.
	 * @param src the original byte array
	 * @return the encoded byte array as a UTF-8 String
	 */
	public static String encodeToString(byte[] src) {
		if (src.length == 0) {
			return "";
		}
		return new String(encode(src), DEFAULT_CHARSET);
	}

	/**
	 * Base64-decode the given byte array from an UTF-8 String.
	 * @param src the encoded UTF-8 String
	 * @return the original byte array
	 */
	public static byte[] decodeFromString(String src) {
		if (src.isEmpty()) {
			return new byte[0];
		}
		return decode(src.getBytes(DEFAULT_CHARSET));
	}

	/**
	 * Base64-encode the given byte array to a String using the RFC 4648
	 * "URL and Filename Safe Alphabet".
	 * @param src the original byte array
	 * @return the encoded byte array as a UTF-8 String
	 */
	public static String encodeToUrlSafeString(byte[] src) {
		return new String(encodeUrlSafe(src), DEFAULT_CHARSET);
	}

	/**
	 * Base64-decode the given byte array from an UTF-8 String using the RFC 4648
	 * "URL and Filename Safe Alphabet".
	 * @param src the encoded UTF-8 String
	 * @return the original byte array
	 */
	public static byte[] decodeFromUrlSafeString(String src) {
		return decodeUrlSafe(src.getBytes(DEFAULT_CHARSET));
	}

}
### 安装 RabbitMQ Server 3.8.32 on CentOS 7CentOS 7安装特定版本的 RabbitMQ Server(如 3.8.32)可以通过下载官方提供的 `.rpm` 包并使用 `yum` 或 `dnf` 进行安装。以下是详细的步骤: 1. **添加 Erlang Solutions 仓库** RabbitMQ 依赖于 Erlang,因此需要先安装合适的 Erlang 版本。可以使用 Erlang Solutions 提供的仓库来安装 Erlang。 ```bash curl -fsSL https://packages.erlang-solutions.com/rpm/centos/erlang_solutions.asc | sudo rpm --import - echo "deb https://packages.erlang-solutions.com/rpm/centos-7-x86_64/ stable main" | sudo tee /etc/yum.repos.d/erlang-solutions.repo ``` 2. **安装 Erlang** 安装适用于 RabbitMQ 的 Erlang 版本。RabbitMQ 3.8.x 需要 Erlang 21.x 到 23.x 之间的版本。 ```bash sudo yum install -y esl-erlang ``` 3. **下载 RabbitMQ 3.8.32 RPM 包** 可以从 [RabbitMQ 官方网站](https://www.rabbitmq.com/releases/rabbitmq-server/) 下载特定版本的 `.rpm` 包。 ```bash wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.32/rabbitmq-server-3.8.32-1.el7.noarch.rpm ``` 4. **安装 RabbitMQ Server** 使用 `yum` 或 `rpm` 命令安装下载的 `.rpm` 包。 ```bash sudo yum install -y rabbitmq-server-3.8.32-1.el7.noarch.rpm ``` 5. **启动 RabbitMQ 服务** 安装完成后,启动 RabbitMQ 服务并设置为开机自启。 ```bash sudo systemctl enable rabbitmq-server sudo systemctl start rabbitmq-server ``` 6. **启用管理插件** 如果需要使用 Web 管理界面,可以启用管理插件。 ```bash sudo rabbitmq-plugins enable rabbitmq_management ``` 7. **创建管理员用户** 创建一个具有管理权限的用户,并为其分配权限。 ```bash sudo rabbitmqctl add_user admin your_password sudo rabbitmqctl set_user_tags admin administrator sudo rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*" ``` 8. **配置防火墙** 如果系统启用了防火墙(如 `firewalld`),需要开放 RabbitMQ 的端口(默认为 5672,管理界面为 15672)。 ```bash sudo firewall-cmd --permanent --add-port=5672/tcp sudo firewall-cmd --permanent --add-port=15672/tcp sudo firewall-cmd --reload ``` 9. **验证安装** 可以通过以下命令检查 RabbitMQ 的状态和集群信息。 ```bash sudo systemctl status rabbitmq-server sudo rabbitmqctl cluster_status ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

languageStudents

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值