public static Map getAcceptContextImgs( String acceptContext ) {
Map fileMap = new HashMap<>(1 << 4);
String num = String.format("%04d", new Random().nextInt(10000));
if (StringUtils.isNotEmpty(acceptContext)) {
List imgList = getImgStr(acceptContext);
if (!org.springframework.util.CollectionUtils.isEmpty(imgList)) {
for (int i = 0; i < imgList.size(); i++) {
if (imgList.get(i).contains("base64,")) {
String fileName = String.format("%s%s%s", num, i, ".jpg");
fileMap.put(base64ToFile(imgList.get(i).split("base64,")[1], fileName), fileName);
}
}
}
}
return fileMap;
}
//BASE64解码成File文件
public static File base64ToFile( String base64, String fileName ) {
File file = null;
//创建文件目录
String filePath = ZEN_TAO_ACCEPTCONTEXT_IMGS_PATH;
File dir = new File(filePath);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
BufferedOutputStream bos = null;
java.io.FileOutputStream fos = null;
try {
byte[] bytes = Base64.getDecoder().decode(base64);
file = new File(filePath + "/" + fileName);
fos = new java.io.FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
log.error("禅道工单富文本图片存储失败0", e);
throw new BaseException("禅道工单富文本图片存储失败");
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
log.error("禅道工单富文本图片存储失败1", e);
throw new BaseException("禅道工单富文本图片存储失败");
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
log.error("禅道工单富文本图片存储失败2", e);
throw new BaseException("禅道工单富文本图片存储失败");
}
}
}
return file;
}
public static List getImgStr( String htmlStr ) {
List list = new ArrayList<>();
Matcher m_image = p_image.matcher(htmlStr);
while (m_image.find()) {
// 得到数据
String img = m_image.group();
// System.out.println(img);
// 匹配中的src数据
Matcher m = r_image.matcher(img);
while (m.find()) {
list.add(m.group(1));
}
}
return list;
}