下载csdn文章,并保存md笔记中的图片链接至本地
有的时候,拿到别人的md笔记,但是笔记中的图片又是以链接的格式给的,这个链接说不定后面就失效了,笔记也就看不到图片了。手动右键也可以保存图片,但是1个1个点太麻烦了,就练习一下正则的使用方法,把图片存下来。一行一行的读取原来的md文档,每一行使用正则拿到匹配的图片链接,并保存到本地。
·
推荐1个下载别人csdn文章笔记的java项目:csdn-blog2markword-downloader
拿到别人的md笔记后,但是笔记中的图片又是以链接的格式给的,这个链接说不定后面就失效了,笔记也就看不到图片了。手动右键也可以保存图片,但是1个1个点太麻烦了,就练习一下正则的使用方法,把图片存下来。
一行一行的读取原来的md文档,每一行使用正则拿到匹配的图片链接,并保存到本地。
package com.zzhua;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Slf4j
public class img {
private static RestTemplate template = new RestTemplate();
public static final String ERR_IMG_PATH_TXT = System.getProperty("user.dir") + "\\err_img_path.txt";
public static RandomAccessFile raf;
static {
try {
File file = new File(ERR_IMG_PATH_TXT);
if (file.exists()) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
file.renameTo(new File(ERR_IMG_PATH_TXT + "." + sdf.format(new Date())));
}
raf = new RandomAccessFile(ERR_IMG_PATH_TXT, "rw");
} catch (Exception e) {
throw new RuntimeException("初始化文件失败");
}
}
public static void main(String[] args) throws IOException {
File file = new File("D:\\myowrk\\Notes\\0.博主md文档\\test");
if (file.isDirectory()) {
handleDirPath(file);
} else {
handleFilePath(file.getAbsolutePath());
}
System.out.println(ERR_IMG_PATH_TXT);
}
public static void handleDirPath(File dir) {
if (dir.exists()) {
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
handleDirPath(file);
} else {
try {
handleFilePath(file.getAbsolutePath());
} catch (IOException e) {
System.err.println("处理失败: " + file.getAbsolutePath());
try {
raf.writeBytes(file.getAbsolutePath() + "\r\n");
raf.write("\r\n".getBytes(Charset.defaultCharset()));
} catch (IOException e2) {
e2.printStackTrace();
System.err.println("写入失败: " + file.getAbsolutePath());
}
}
}
}
}
}
public static void handleFilePath(String filePath) throws IOException{
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("11121");
// System.out.println(matcher.find());
// System.out.println(matcher.group(1)); // 文件名
// 
String standardFormat = "";
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.info("获取图片, 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);
}
}
}
更多推荐
所有评论(0)