From 0d49efb3af9a2c2bac1db228861a91987bd0d251 Mon Sep 17 00:00:00 2001 From: luoxiang <2806718453@qq.com> Date: Mon, 7 Jan 2019 23:43:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E6=9C=AC=E5=9C=B0=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E8=BD=AC=E6=8D=A2=E4=B8=BAgithub=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- z-utils/ChangeImageUrl.java | 71 +++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 z-utils/ChangeImageUrl.java diff --git a/z-utils/ChangeImageUrl.java b/z-utils/ChangeImageUrl.java new file mode 100644 index 0000000..8dc34fe --- /dev/null +++ b/z-utils/ChangeImageUrl.java @@ -0,0 +1,71 @@ +import java.io.*; +import java.util.ArrayList; +import java.util.List; + +/** + * @author : heibaiying + * @description : 转换本地图片为Github图片工具类 + */ +public class ChangeImageUrl { + + public static void main(String[] args) throws Exception { + + if (args.length < 1) { + System.out.println("请传递路径"); + return; + } + + String dir = args[0]; + + String preUrl = "https://github.com/heibaiying/spring-samples-for-all/blob/master/pictures/"; + String regex = "(!\\[(\\S*)]\\(D:\\\\spring-samples-for-all\\\\pictures\\\\(\\S*)\\)[^(
)]*?)"; + + List filesList = getAllFile(dir, new ArrayList<>()); + for (String filePath : filesList) { + changeImageUrl(filePath, preUrl, regex); + } + System.out.println("图片地址转换成功!"); + } + + + private static void changeImageUrl(String filePath, String preUrl, String oldImageUrlRegex) throws IOException { + + FileReader reader = new FileReader(filePath); + StringBuilder stringBuilder = new StringBuilder(); + char[] chars = new char[1024 * 1024]; + int read = 0; + while ((read = reader.read(chars)) != -1) { + stringBuilder.append(new String(chars, 0, read)); + } + reader.close(); + String content = stringBuilder.toString(); + //github 居中方式
+ String newContent = content.replaceAll(oldImageUrlRegex, + String.format("
", preUrl)); + FileWriter fileWriter = new FileWriter(new File(filePath)); + fileWriter.write(newContent); + fileWriter.flush(); + + } + + private static List getAllFile(String dir, List filesList) { + File file = new File(dir); + //如果是文件 则不遍历 + if (file.isFile() && file.getName().endsWith(".md")) { + filesList.add(file.getAbsolutePath()); + } + //如果是文件夹 则遍历下面的所有文件 + File[] files = file.listFiles(); + if (files != null) { + for (File f : files) { + if (f.isDirectory() && !f.getName().startsWith(".")) { + getAllFile(f.getAbsolutePath(), filesList); + } else if (f.getName().endsWith(".md")) { + filesList.add(f.getAbsolutePath()); + } + } + } + return filesList; + } + +} \ No newline at end of file