String s = shopProduct.getShopProductId()+"-wxqrcode.png";
//需要转换的base64字符串
String base64 = "XXX";
//通过decode转换为byte数组
byte[] bytes1 = Base64.decodeBase64(base64); //package org.apache.commons.codec.binary.Base64;
//调用封装的方法 得到file
File file = byte2File(bytes1, "", s);
//上传S3 得到该base64字符串所对应的图片url
UploadResult upload = ironhideStorageClient.upload(file);
String s3Url= endpoint+"/"+bucketName +"/"+ upload.getFileName();
System.out.println(s3Url);
public static File byte2File(byte[] buf, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {
dir.mkdirs();
}
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
bos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
return file;
}
base64转byte数组再转File文件
最新推荐文章于 2024-04-01 11:28:24 发布