1.前言
之前在学校一直记笔记都是直接写mark down文件存储在本地,笔记中的图片基本上都是直接复制进去,而使用的md编辑器typora也是默认图片存储在本地,由于基本上也只用自己电脑,也没有啥写博客的习惯,所以并没有使用图床之类的东西,但后来遇到不使用自己电脑但需要看笔记的场景,就开始考虑将笔记存储到云端了
我开始准备存储到语雀知识库上,但发现导入的md文件图片需要自己上传或者复制到网站对应编辑器处,如果图片很多的话就十分麻烦,于是打算写个代码将md文件中图片上传,代码在下面,因为我目前习惯将本地md文件本地图片存储一份,所以并没有使用picGo之类的,结合typora直接设置为复制时上传图片,这样没网的时候也能看
2.代码
该代码只是将文件中的本地md文件将图片链接上传并更换在同目录下输出转换后的文件,在原文件名的基础上增加"-uplaod后缀",且我使用的是阿里云oss存储,若不会使用oss,只需要简单看一下教程将我代码中的upload函数对应oss的endpoint accessKey等改为自己的即可,若要进行批量文件的上传,或者图片格式与我本地的图片默认格式不同等不同处等,可自行做对应修改
我的本地图片格式
![image-20231204102925247](D:\desktop\tmp.assets\image-20231204102925247.png)
public static void main(String[] args) throws Exception{
//todo 自行更改md文件路径
trans("D:\\desktop\\tmp.md");
}
public static String upload(String path, String fileName) {
//todo 使用自己oss配置
String endpoint = "oss-cn-beijing.aliyuncs.com";
String accessKeyId = "xxxxxxxxxxxx";
String accessKeySecret = "xxxxxxxxxxxxxx";
String bucketName = "xxxxxxxxxxx";
String url = null;
OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
PutObjectResult result = oss.putObject(bucketName, fileName, new FileInputStream(path));
url = String.format("https://%s.%s/%s", bucketName, endpoint, fileName);
} catch (FileNotFoundException e) {
System.out.println("文件不存在3");
} catch (Exception e) {
System.out.println("OSS服务错误");
e.printStackTrace();
}
oss.shutdown();
return url;
}
public static void trans(String filePath) {
File sFile = new File(filePath);
if (!sFile.exists()) {
System.out.println("文件不存在1");
return;
}
// 获得文件路径,并为生成文件名后加上 "-upload"
int idx = filePath.lastIndexOf("/") + 1;
// 兼容分隔符"/"和"\"
if (idx == 0) {
idx = filePath.lastIndexOf("\\") + 1;
}
String dFileName = filePath.substring(idx);
String dFilePath = filePath.substring(0, idx);
idx = dFileName.lastIndexOf(".");
dFileName = dFileName.substring(0, idx) + "-upload" + dFileName.substring(idx);
System.out.println("输出文件名和路径:");
System.out.println(dFileName + " " + dFilePath);
File dFile = new File(dFilePath + dFileName);
// 创建文件
if (!dFile.exists()) {
try {
boolean flag = dFile.createNewFile();
} catch (IOException e) {
System.out.println("创建输出文件失败");
e.printStackTrace();
}
}
try (BufferedReader br = new BufferedReader(new FileReader(sFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(dFile))
) {
String line, newLine;
// 匹配md文件中本地图片的路径,图片格式如下
// ![image-20231204102925247](D:\desktop\tmp.assets\image-20231204102925247.png)
Pattern pattern = Pattern.compile("!\\[image-\\d{17}\\]\\([\\w[^_]]:.+\\)");
System.out.println("匹配到的图片路径所在行:");
while ((line = br.readLine()) != null) {
newLine = line;
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String pic = matcher.group();
String picPath = pic.substring(27, pic.length() - 1);
String picName = picPath.substring(picPath.lastIndexOf("\\") + 1);
if (idx == 0) {
idx = filePath.lastIndexOf("/") + 1;
}
String picUrl = upload(picPath, picName);
if (picUrl == null) {
System.out.println("图片上传失败");
return;
}
newLine = newLine.replace(pic, pic.substring(0, 27) + picUrl + ")");
System.out.println(newLine);
}
bw.write(newLine);
bw.newLine();
}
System.out.println("转换完成");
} catch (FileNotFoundException e) {
System.out.println("文件不存在2");
} catch (IOException e) {
System.out.println("IO错误");
}
}