Base64是一种将二进制数据编码成文本格式的算法。在Java中,Base64编码可以通过Java自带的java.util.Base64类来实现。
Base64编码和解码
在Java中,Base64编码和解码可以通过Base64类的静态方法来实现。
编码
要进行Base64编码,需要将待编码的二进制数据转换成字节数组,然后调用Base64类的encodeToString()方法,将字节数组作为参数传递进去。
以下是一个简单的Base64编码示例:
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) {
String originalInput = "Hello World!";
byte[] inputBytes = originalInput.getBytes();
String encodedString = Base64.getEncoder().encodeToString(inputBytes);
System.out.println("Encoded String: " + encodedString);
}
}
在上面的示例中,我们将字符串"Hello World!"转换成字节数组,然后调用Base64.getEncoder().encodeToString()方法对其进行Base64编码。运行代码后,输出结果如下:
Encoded String: SGVsbG8gV29ybGQh
解码
要进行Base64解码,需要将Base64编码后的字符串转换成字节数组,然后调用Base64类的decode()方法,将字节数组作为参数传递进去。
以下是一个简单的Base64解码示例:
import java.util.Base64; public class Base64Example { public static void main(String[] args) { String encodedString = "SGVsbG8gV29ybGQh"; byte[] decodedBytes = Base64.getDecoder().decode(encodedString); String decodedString = new String(decodedBytes); System.out.println("Decoded String: " + decodedString); }
}
在上面的示例中,我们将字符串"SGVsbG8gV29ybGQh"进行Base64解码,然后将解码后的字节数组转换成字符串输出。运行代码后,输出结果如下:
Decoded String: Hello World!
URL和MIME编码
在Java中,Base64编码还有两种变体:URL编码和MIME编码。
URL编码
URL编码是一种特殊的Base64编码,用于将数据安全地传输到URL中。URL编码使用Base64编码的字符集,但将"+"替换为"-",将"/"替换为"_",并且省略了末尾的"="。
要进行URL编码,可以调用Base64类的getUrlEncoder()方法。以下是一个简单的URL编码示例:
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) {
String originalInput = "https://www.example.com/path/to/resource?key=value";
byte[] inputBytes = originalInput.getBytes();
String encodedString = Base64.getUrlEncoder().encodeToString(inputBytes);
System.out.println("Encoded String: " + encodedString);
}
}
在上面的示例中,我们将URL字符串"https://www.example.com/path/to/resource?key=value"转换成字节数组,然后调用Base64.getUrlEncoder().encodeToString()方法对其进行URL编码。运行代码后,输出结果如下:
Encoded String: aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vcGF0aC90by9yZXNvdXJjZT9rZXk9dmFsdWU
MIME编码
MIME编码也是一种特殊的Base64编码,通常用于将二进制数据嵌入到文本邮件中。MIME编码使用Base64编码的字符集,但将每行限制为76个字符,并在每行末尾添加换行符"\r\n"。
要进行MIME编码,可以调用Base64类的getMimeEncoder()方法。以下是一个简单的MIME编码示例:
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) {
String originalInput = "This is some binary data that needs to be encoded in MIME format.";
byte[] inputBytes = originalInput.getBytes();
String encodedString = Base64.getMimeEncoder().encodeToString(inputBytes);
System.out.println("Encoded String: " + encodedString);
}
}
在上面的示例中,我们将字符串"This is some binary data that needs to be encoded in MIME format."转换成字节数组,然后调用Base64.getMimeEncoder().encodeToString()方法对其进行MIME编码。运行代码后,输出结果如下:
Encoded String: VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhIHRoYXQgbmVlZHMgdG8gYmUgZW5jb2RlZCBpbiBNSU1FIGZvcm1hdC4K
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) {
String originalInput = "This is some binary data that needs to be encoded with a custom charset.";
byte[] inputBytes = originalInput.getBytes(StandardCharsets.UTF_8);
// Use custom character set
Base64.Encoder encoder = Base64.getEncoder().withoutPadding().onReplacement('?');
String encodedString = encoder.encodeToString(inputBytes);
System.out.println("Encoded String: " + encodedString);
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) throws IOException {
String originalInput = "This is some binary data that needs to be encoded in a stream.";
byte[] inputBytes = originalInput.getBytes();
// Stream encoding
Base64.Encoder encoder = Base64.getEncoder();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Base64OutputStream base64OutputStream = new Base64OutputStream(outputStream, encoder);
base64OutputStream.write(inputBytes);
base64OutputStream.flush();
base64OutputStream.close();
String encodedString = outputStream.toString();
System.out.println("Encoded String: " + encodedString);
}
}
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Base64;
public class Base64Example {
public static void main(String[] args) throws IOException {
String encodedString = "VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhIHRoYXQgbmVlZHMgdG8gYmUgZW5jb2RlZCBpbiBhIHN0cmVhbS4=";
byte[] decodedBytes = Base64.getDecoder().decode(encodedString.getBytes());
// Stream decoding
Base64.Decoder decoder = Base64.getDecoder();
ByteArrayInputStream inputStream = new ByteArrayInputStream(decodedBytes);
Base64InputStream base64InputStream = new Base64InputStream(inputStream, decoder);
byte[] buffer = new byte[1024];
int bytesRead;
StringBuilder output = new StringBuilder();
while ((bytesRead = base64InputStream.read(buffer)) != -1) {
output.append(new String(buffer, 0, bytesRead));
}
System.out.println("Decoded String: " + output.toString());
}
}
总结
Social Plugin