fix(asms): 截取问题

This commit is contained in:
yjz
2024-03-02 13:24:52 +08:00
parent 32cf08b2d7
commit 11b0fc7e7a
2 changed files with 48 additions and 19 deletions
@@ -856,6 +856,16 @@ public class LawsAsmsOpenApiServiceImpl extends ServiceImpl<LawsAsmsOpenApiMappe
while (matcher.find()) {
String imageUrl = matcher.group(1);
int endIndex = 0;
try {
endIndex = imageUrl.indexOf("\" alt");
}catch (Exception e){
try {
endIndex = imageUrl.indexOf("\" />");
}catch (Exception e2){
endIndex = imageUrl.indexOf("\" >");
}
}
String urlSuffix = imageUrl.substring(imageUrl.indexOf("getImg"),
imageUrl.indexOf("\" alt"));
String url = asmsPostUtil.getHost() + urlSuffix;
@@ -598,26 +598,45 @@ public class SynchronizationAsmsService {
stringBuilder.append("<p>").append(articleContent).append("</p>");
articleContent = stringBuilder.toString();
}else {
String urlSuffix = articleContent.substring(articleContent.indexOf("getImg"),
articleContent.indexOf("\" alt"));
String imageUrl = asmsPostUtil.getHost() + urlSuffix;
String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
// 获取图片
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.USER_AGENT, "Mozilla/5.0");
headers.set(HttpHeaders.ACCEPT, MediaType.ALL_VALUE);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<byte[]> responseEntity = restTemplate.exchange(imageUrl, HttpMethod.GET, entity, byte[].class);
if (HttpStatus.OK == responseEntity.getStatusCode()) {
byte[] imageBytes = responseEntity.getBody();
MultipartFile file = new MockMultipartFile(fileName, fileName, "application/octet-stream", imageBytes);
// 上传图片
OSSFile oSSFile = ossFileService.uploadLocalForSplit(file, "", "", "");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<p>").append("<img class=\"wordImg\" style=\"width:100%;height:100%\" src=\"")
.append(oSSFile.getUrl()).append("\">").append("</p>");
articleContent = stringBuilder.toString();
// 定义正则表达式匹配 img 标签及其属性值
Pattern pattern = Pattern.compile("(<\\s*img[^>]*>)");
Matcher matcher = pattern.matcher(articleContent);
while (matcher.find()) {
String imageUrl = matcher.group(1);
int endIndex = 0;
try {
endIndex = imageUrl.indexOf("\" alt");
}catch (Exception e){
try {
endIndex = imageUrl.indexOf("\" />");
}catch (Exception e2){
endIndex = imageUrl.indexOf("\" >");
}
}
String urlSuffix = imageUrl.substring(imageUrl.indexOf("getImg"),
endIndex);
String url = asmsPostUtil.getHost() + urlSuffix;
String fileName = url.substring(url.lastIndexOf("/") + 1);
// 获取图片
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.USER_AGENT, "Mozilla/5.0");
headers.set(HttpHeaders.ACCEPT, MediaType.ALL_VALUE);
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, entity, byte[].class);
if (HttpStatus.OK == responseEntity.getStatusCode()) {
byte[] imageBytes = responseEntity.getBody();
MultipartFile file = new MockMultipartFile(fileName, fileName,"application/octet-stream" ,imageBytes);
// 上传图片
OSSFile oSSFile = ossFileService.uploadLocalForSplit(file, "", "", "");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<img class=\"wordImg\" style=\"width:100%;height:100%\" src=\"")
.append(oSSFile.getUrl()).append("\">");
// 在此处进行替换操作,将图片链接替换为新的地址或者删除该图片标签等
articleContent = articleContent.replaceAll(imageUrl,stringBuilder.toString());
}
}
}
}
return articleContent;