推荐1个下载别人csdn文章笔记的java项目:csdn-blog2markword-downloader
拿到别人的md笔记后,但是笔记中的图片又是以链接的格式给的,这个链接说不定后面就失效了,笔记也就看不到图片了。手动右键也可以保存图片,但是1个1个点太麻烦了,就练习一下正则的使用方法,把图片存下来。
一行一行的读取原来的md文档,每一行使用正则拿到匹配的图片链接,并保存到本地。
@Slf4j
public class TestReg {
private static RestTemplate template = new RestTemplate();
public static void main(String[] args) throws IOException {
String filePath = "D:\\documents\\黑马rabbitmq\\mq基础\\MQ基础.md";
File file = new File(filePath);
// 创建同级目录下的assets文件夹
File assetsFile = new File(file.getParent() + "\\assets");
if (assetsFile.exists() || !assetsFile.isDirectory()) {
assetsFile.mkdir();
}
RandomAccessFile raf = new RandomAccessFile(file.getParent() + "\\" + "_" + file.getName(), "rw");
// 匹配图片链接所使用的正则
// !\[.*?]\((https?:.*/(.*?\.(png|jpg|jpeg)))
String imgLinkPattern = "!\\[.*?]\\((https?:.*/(.*?\\.(png|jpg|jpeg)))";
Pattern p = Pattern.compile(imgLinkPattern);
// Matcher matcher = p.matcher("11![image.png](https://cdn.nlark.com/yuque/0/2023/png/27967491/1686983181054-f2bcce85-1fce-412f-95cd-1ae829f8406f.png#averageHue=%239dce6d&clientId=uf9c47826-2719-4&from=paste&height=613&id=u84c8f02e&originHeight=760&originWidth=1695&originalType=binary&ratio=1.2395833730697632&rotation=0&showTitle=false&size=112976&status=done&style=none&taskId=u779e4d59-c9a8-4b1f-a49f-59c578c4ccd&title=&width=1367.3949141495996)121");
// System.out.println(matcher.find());
// System.out.println(matcher.group(1)); // 文件名
// ![文件名](assets/文件名)
String standardFormat = "![文件名](assets/文件名)";
InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
BufferedReader bufReader = new BufferedReader(reader);
String line = null;
while ((line = bufReader.readLine()) != null) {
Matcher matcher = p.matcher(line);
if (matcher.find()) {
String fileName = matcher.group(2);
String imgUrl = matcher.group(1);
String standardFileName = standardFormat.replaceAll("文件名", fileName);
// System.out.println(imgUrl);
// System.out.println(fileName);
// System.out.println(standardFileName);
saveImgToLocal(imgUrl, fileName, file.getParent());
raf.write(standardFileName.getBytes(Charset.defaultCharset()));
} else {
raf.write(line.getBytes(Charset.defaultCharset()));
}
raf.write("\r\n".getBytes(Charset.defaultCharset()));
}
raf.close();
}
public static void saveImgToLocal(String imgUrl, String fileName, String dir) {
log.error("获取图片, imgUrl: {}, fileName: {}, dir", imgUrl, fileName, dir);
try {
Resource resource = template.getForObject(imgUrl, Resource.class);
FileOutputStream fos = new FileOutputStream(dir + "\\assets\\" + fileName);
StreamUtils.copy(resource.getInputStream(), fos);
fos.close();
} catch (Exception e) {
log.error("获取图片失败, imgUrl: {}, fileName: {}, dir", imgUrl, fileName, dir);
}
}
}