提交 1d03540e 作者: yanxin

资讯标编辑新增base64转图片处理

上级 fb64d8ca
......@@ -20,8 +20,6 @@ import com.zzsn.event.es.EsService;
import com.zzsn.event.mapper.SubjectMapper;
import com.zzsn.event.service.*;
import com.zzsn.event.util.*;
import com.zzsn.event.util.tree.Node;
import com.zzsn.event.util.user.AuthUtil;
import com.zzsn.event.util.user.UserUtil;
import com.zzsn.event.util.user.UserVo;
import com.zzsn.event.vo.*;
......@@ -92,6 +90,8 @@ public class InformationServiceImpl implements InformationService {
private SysDictItemService sysDictItemService;
@Autowired
private PythonUtil pythonUtil;
@Autowired
private ImageUtil imageUtil;
private String subjectId = "1898653164373065730";//中外智库专栏对应专题id
@Override
......@@ -629,10 +629,14 @@ public class InformationServiceImpl implements InformationService {
if (StringUtils.isNotBlank(specialInformation.getContentWithTag())) {
String content = Jsoup.parse(specialInformation.getContentWithTag()).text();
specialInformation.setContent(content);
//base64图片转url
specialInformation.setContentWithTag(imageUtil.changeBase64ToUrl(specialInformation.getContentWithTag()));
}
if (StringUtils.isNotBlank(specialInformation.getContentWithTagRaw())) {
String contentRaw = Jsoup.parse(specialInformation.getContentWithTagRaw()).text();
specialInformation.setContentRaw(contentRaw);
//base64图片转url
specialInformation.setContentWithTag(imageUtil.changeBase64ToUrl(specialInformation.getContentWithTag()));
}
//处理时间格式
specialInformation.setPublishDate(EsDateUtil.esFieldDateFormat(specialInformation.getPublishDate()));
......
package com.zzsn.event.util;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class ImageUtil {
@Value("${util.getUrlPath:https://clb.ciglobal.cn/clb-api/common/getUrlByBase64Data}")
private String getUrlPath;
/**
* base64图片转url
* @param html
*/
public String changeBase64ToUrl(String html){
try {
Document htmlDoc = Jsoup.parse(html);
Elements imgs = htmlDoc.select("img"); // 获取所有的<img>标签
boolean flag = false;
for (Element img : imgs) {
// 获取src属性
String baseData = img.attr("src");
if (baseData!=null && baseData.startsWith("data:image/")){
//base64图片,需要转为url存储
String post = HttpUtil.post(getUrlPath, baseData);
JSONObject jsonObject = JSON.parseObject(post);
if(jsonObject.getInteger("code") == 200){
log.info("base64图片转url成功:{}", jsonObject.getString("result"));
img.attr("src", jsonObject.getString("result"));
flag = true;
}
}
}
if(flag){
html = htmlDoc.html();
}
}catch (Exception e){
log.error("base64图片转url失败:{}", e.getMessage());
}
return html;
}
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论