由于Gitee作为图床的仓库被禁止开源,导致图片无法访问,所以转用Github作为图床。
下载图片文件并上传
(1)将作为图床的Gitee仓库下载到本地
(2)新建一个Github仓库作为图床,具体创建过程可参考CDN加速GitHub搭配PicGo自建图床
(3)执行如下步骤将下载的文件中的所有图片上传到Github
1 2 3 4 5
| 1. 克隆:git clone git@github.com:username/cdnPicture.git 2. 复制你下载的图片到cdnPicture中 3. 添加到暂存区:git add . 4. 把暂存区的所有更改提交到了本地仓库中:git commit -m"init all imgs" 5. 提交main分支到github上:git push origin main
|
CDN加速GitHub搭配PicGo自建图床
详细步骤不多说了,具体参考CDN加速GitHub搭配PicGo自建图床。
这里要提一句,使用https://fastly.jsdelivr.net/
,和使用https://cdn.jsdelivr.net/
都可,原来的图床图片地址为:https://raw.githubusercontent.com/wxler/cdnPicture/main/imgs/20220605212813.png。
替换后为:
批量替换文件中的图片url
如果只替换一个,那么只有写一个from和一个to变量即可。
注意:为了防止出错,替换前记得备份
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| package replaceMD;
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner;
public class ChangePicPath { private static String folderName = "D:\\A_blog\\测试"; private static String from = "\\]\\(https://gitee.com/wxler/blogimg/raw/master/imgs/"; private static String to = "\\]\\(https://fastly.jsdelivr.net/gh/wxler/cdnPicture/imgs/";
private static String from222 = "\\]\\(https://gitee.com/wxler/define_img/raw/master/imgs/20210925/"; private static String to222 = "\\]\\(https://fastly.jsdelivr.net/gh/wxler/cdnPicture/imgs/20210925/";
private static List<String> errFileNameList = new ArrayList<>();
public static void main(String[] args) throws IOException {
List<String> pathList = getAllFileName(folderName); System.out.println("成功获取所有文件名称!文件总数为:"+pathList.size());
System.out.println("第一个文件名称为:"+pathList.get(0)); System.out.println("输入任意数字,再按回车键继续..."); new Scanner(System.in).nextInt(); int errCount = 0; for (String path : pathList) { boolean f1=changeFileContent(from,to,path); boolean f2=changeFileContent(from222,to222,path); if (!f1 || !f2) { errCount++; }
} System.out.println("出错文件数为:"+errCount); System.out.println("所有出错文件名为:"); for (String errPath : errFileNameList) { System.out.println(errPath); }
}
private static List<String> getAllFileName(String folderName) { ArrayList<String> filePathList = new ArrayList<>(); dfs(folderName,filePathList); return filePathList; }
private static void dfs(String path, List<String>filePathList) { File file = new File(path); if (file.isFile() && file.getName().endsWith(".md")) { filePathList.add(file.getAbsolutePath()); return; } if (file.isDirectory()) { File[] files = file.listFiles(); for (File tmpFile : files) { dfs(tmpFile.getAbsolutePath(),filePathList); } } }
private static boolean changeFileContent(String from,String to,String filePath) { File file = new File(filePath); try { FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader);
CharArrayWriter tempStream = new CharArrayWriter();
String line = null;
while ((line = bufferedReader.readLine()) != null) { line = line.replaceAll(from,to); tempStream.write(line); tempStream.append(System.getProperty("line.separator")); }
bufferedReader.close(); FileWriter out = new FileWriter(file); tempStream.writeTo(out); out.close(); } catch (IOException e) { errFileNameList.add(filePath); return false; } return true; } }
|