Base64码常见操作(url链接文件转base64编码、本地文件转base64编码等)
【摘要】 引入坐标 <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <ar...
引入坐标
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
常见操作
下载远程url的文件并转换成base64编码
代码如下:
public static String file(String url){
String encode = null;
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
CloseableHttpResponse response = client.execute(get);
//文件流
HttpEntity httpEntity = response.getEntity();
InputStream inStream = httpEntity.getContent();
byte[] bytes = IOUtils.toByteArray(inStream);
//附件base64
encode = cn.hutool.core.codec.Base64.encode(bytes);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return encode;
}
测试:
/**
* 测试下载远程url的文件,转换成base64编码
* @throws Exception
*/
@Test
public void testUrlFileToBase64() throws Exception {
String BASE64Str = Base64Util.file("https://xxx.oss-cn-hangzhou.aliyuncs.com/upload/20220513/165242342M");
System.out.println("BASE64Str:"+BASE64Str);
}
因为是图片,可以复制输出的base64编码到这个网址验证:https://tool.jisuapi.com/base642pic.html
其他base64操作如下:
文件转base64编码
public static String fileToBase64(File file) {
try {
byte[] imageData = FileUtils.readFileToByteArray(file);
String base64 = encodeImage(imageData);
return base64;
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
public static String encodeImage(byte[] imageData) {
String encodeStr = Base64.getEncoder().encodeToString(imageData);
log.info("encodeImage>>" + encodeStr);
return encodeStr;
}
判断一个字符串是否是base64
public static boolean isBase64(String str) {
String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
return Pattern.matches(base64Pattern, str);
}
方式一:jdk的工具类javax.xml.bind.DatatypeConverter
//方式一:jdk的工具类javax.xml.bind.DatatypeConverter
String str = "base64字符串";
//byte[]转base64
String encode = DatatypeConverter.printBase64Binary(str.getBytes());
System.out.println("转base64后:"+encode);
//Base64转byte[]
byte[] decode= DatatypeConverter.parseBase64Binary(encode);
System.out.println("base64转回:"+new String(decode));
方式二:jdk的工具类sun.misc.BASE64Decoder和sun.misc.BASE64Encoder
//方式二:jdk的工具类sun.misc.BASE64Decoder和sun.misc.BASE64Encoder
//byte[]转base64
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode1 = base64Encoder.encode(str.getBytes());
System.out.println("转base64后:"+encode1);
//Base64转byte[]
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] decodeBuffer = base64Decoder.decodeBuffer(encode1);
System.out.println("base64转回:"+new String(decodeBuffer));
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)