CSDN外链图片转存失败
有点坑,不仅有图片转存失败的问题,还限制每日发布的文章数量,保存草稿还发布频繁,无语
主要是通过程序将
![]() 转
<img src=""/>
这个前提是你的提示是可以网络访问的
程序如下
主要是这两行代码需要修改
// 原有文件目录
File baseDir = new File("D:\\17736\\Desktop\\blog2");
getAllFile(baseDir);
// 新文件目录
File copyDir = new File(file.getPath().replace("blog2", "blog3"));
public class App1 {
// 图片的统一前缀
static final String imageUrl = "https://cdn.jsdelivr.net/gh/cloudinwinter/myimage@master/blogImg";
static List<File> fileList = new ArrayList<>();
public static void main(String[] args) throws IOException {
// 原有文件目录
File baseDir = new File("D:\\17736\\Desktop\\blog2");
getAllFile(baseDir);
for (File file : fileList) {
BufferedWriter bufferedWriter = null;
BufferedReader bufReader = null;
OutputStreamWriter outputStreamWriter = null;
try {
//读取文件
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
bufReader = new BufferedReader(inputStreamReader);
// 新文件目录
File copyDir = new File(file.getPath().replace("blog2", "blog3"));
if (!copyDir.exists()) {
File parentFile = copyDir.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
copyDir.createNewFile();
}
// 输出文件
FileOutputStream fileOutputStream = new FileOutputStream(copyDir);
outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
bufferedWriter = new BufferedWriter(outputStreamWriter);
String line = "";
while ((line = bufReader.readLine()) != null) {
String replace = replace(line);
bufferedWriter.write(replace);
bufferedWriter.newLine();
}
System.out.println(copyDir.getPath());
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
bufReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
bufferedWriter.close();
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static void getAllFile(File baseDir) {
if (baseDir.isFile()) {
return;
}
File[] files = baseDir.listFiles();
for (File file : files) {
if (file.isFile()) {
fileList.add(file);
}
getAllFile(file);
}
}
private static String replace(String line) {
if (line.contains(imageUrl)) {
int i = line.indexOf(imageUrl);
String substring = line.substring(i, line.length() - 1);
return "<img src=\"" + substring + "\"/>";
}
return line;
}
}
关联信息
- 关联的主题:
- 上一篇:
- 下一篇:
- image: 20221021/1
- 转载自: