将字符串中的64编码转成图片,上传阿里云

来源:互联网 发布:gdi高级编程 pdf 编辑:程序博客网 时间:2024/03/29 06:41


/**
* 将字符串中的64编码转成图片,上传阿里云
* @return
*/
public static String handleBase64ToImg(String content,String id){
content = content.replaceAll("<!--(.*?)-->", "");//删除内容体中的注释
String url = null;
Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>",Pattern.CASE_INSENSITIVE);
String contentStr = StringEscapeUtils.unescapeJavaScript(content);
Matcher m = p.matcher(contentStr);
List<String> srcList = new ArrayList<String>();
while (m.find()) {
url = m.group(1);//去正则中的第一组数据
if(srcList.contains(url)){//排除重复的数据
continue;
}
srcList.add(url);
}
// 对字节数组字符串进行Base64解码并生成图片
if (CollectionUtils.isEmpty(srcList)){// 图像数据为空
return content;
} else {
BASE64Decoder decoder = new BASE64Decoder();
for(int i = 0;i < srcList.size();i++){
try {
// Base64解码  srcList.get(0).split(",")[1]  customTopicImage
byte[] imgStream = decoder.decodeBuffer(srcList.get(i).split(",")[1]);//data:image/octet-stream;base64,iVBORw0KG ","后为正式的流数据
Long fileSize = (long) imgStream.length;//图片文件大小
/*for (int j = 0; j < b.length; ++j) {
if (imgStream[j] < 0) {// 调整异常数据
imgStream[j] += 256;
}
}*/
// 生成jpeg图片
//String imgFilePath = "d://"+id+"_"+i+".jpg";// 新生成的图片
//content = content.replace(fileSteam, "http://customTopicImageName//customTopicImage//"+id+"_"+i+".jpg");
String imgUrl = "http://abcd3.oss-cn-hangzhou.aliyuncs.com/";
String key = "customTopicImage/"+id+"_"+i+".jpg";
content = content.replace(srcList.get(i), imgUrl+key);
OSSUtils.putObjectBySteam(createParam(),imgStream ,key );
/*OutputStream out = new FileOutputStream(imgFilePath);
out.write(imgStream);
out.flush();
out.close();*/
} catch (Exception e) {
return content;
}
}
}
return content;
}
0 0
原创粉丝点击