码迷,mamicode.com
首页 > 编程语言 > 详细

java将base64编码字符串还原为字节数组

时间:2020-03-27 21:47:34      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:base   上传   new   str   base64   ram   ring   rand   common   

 /**
* 图片上传
* @param imgBase
* @return
*/
@PostMapping("/upload")
public CommonResult upload(String imgBase){
byte[] b1 = null;
BASE64Decoder decoder = new BASE64Decoder();
try{
if (imgBase.indexOf("data:image/jpeg;base64,") != -1) {
b1 = decoder.decodeBuffer(imgBase.replaceAll("data:image/jpeg;base64,", ""));
} else {
if (imgBase.indexOf("data:image/png;base64,") != -1) {
b1 = decoder.decodeBuffer(imgBase.replaceAll("data:image/png;base64,", ""));
} else {
b1 = decoder.decodeBuffer(imgBase.replaceAll("data:image/jpg;base64,", ""));
}
}
for (int i = 0; i < b1.length; ++i) {
if (b1[i] < 0) {// 调整异常数据
b1[i] += 256;
}
}
}catch(Exception e){
e.printStackTrace();
}
//添加图片url;
String name = UUID.randomUUID().toString().replace("_", "");
try {
String upload = new QiniuUploadUtil().upload(name, b1);
System.out.println("uploaduploadupload"+upload);
if(imgUrl==null){
imgUrl=new ArrayList<>();
}
imgUrl.add(upload);
} catch (Exception e) {
e.printStackTrace();
}
return CommonResult.success("1");
}
}

java将base64编码字符串还原为字节数组

标签:base   上传   new   str   base64   ram   ring   rand   common   

原文地址:https://www.cnblogs.com/xiaofengshan/p/12584159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!