CSDN外链图片转存失败

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
  • 转载自:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值