讲一个url链接转为base64
时间: 2023-11-18 10:46:55 浏览: 23
当您将URL链接转换为Base64字符串时,您需要先将链接转换为字节数组,然后再使用Base64编码器将其转换为Base64字符串。以下是一个示例Java代码段,可以将URL链接转换为Base64字符串:
```
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class URLtoBase64 {
public static void main(String[] args) throws Exception {
String url = "https://www.google.com";
// Convert URL string to byte array
byte[] urlBytes = url.getBytes(StandardCharsets.UTF_8);
// Encode bytes to Base64 string
String base64String = Base64.getEncoder().encodeToString(urlBytes);
System.out.println("Base64 encoded URL: " + base64String);
}
}
```
在此示例中,我们首先将URL字符串转换为UTF-8字节数组。然后,我们使用`Base64`类中的`getEncoder()`方法获得一个Base64编码器,并使用它将字节数组编码为Base64字符串。最后,我们将结果打印到控制台。