下载csdn文章,并保存md笔记中的图片链接至本地

推荐1个下载别人csdn文章笔记的java项目:csdn-blog2markword-downloader

拿到别人的md笔记后,但是笔记中的图片又是以链接的格式给的,这个链接说不定后面就失效了,笔记也就看不到图片了。手动右键也可以保存图片,但是1个1个点太麻烦了,就练习一下正则的使用方法,把图片存下来。

一行一行的读取原来的md文档,每一行使用正则拿到匹配的图片链接,并保存到本地。

@Slf4j
public class TestReg {

    private static RestTemplate template = new RestTemplate();

    public static void main(String[] args) throws IOException {

        String filePath = "D:\\documents\\黑马rabbitmq\\mq基础\\MQ基础.md";

        File file = new File(filePath);

        // 创建同级目录下的assets文件夹
        File assetsFile = new File(file.getParent() + "\\assets");
        if (assetsFile.exists() || !assetsFile.isDirectory()) {
            assetsFile.mkdir();
        }

        RandomAccessFile raf = new RandomAccessFile(file.getParent() + "\\" + "_" + file.getName(), "rw");

        // 匹配图片链接所使用的正则
        // !\[.*?]\((https?:.*/(.*?\.(png|jpg|jpeg)))
        String imgLinkPattern = "!\\[.*?]\\((https?:.*/(.*?\\.(png|jpg|jpeg)))";

        Pattern p = Pattern.compile(imgLinkPattern);
        // Matcher matcher = p.matcher("11![image.png](https://cdn.nlark.com/yuque/0/2023/png/27967491/1686983181054-f2bcce85-1fce-412f-95cd-1ae829f8406f.png#averageHue=%239dce6d&clientId=uf9c47826-2719-4&from=paste&height=613&id=u84c8f02e&originHeight=760&originWidth=1695&originalType=binary&ratio=1.2395833730697632&rotation=0&showTitle=false&size=112976&status=done&style=none&taskId=u779e4d59-c9a8-4b1f-a49f-59c578c4ccd&title=&width=1367.3949141495996)121");

        // System.out.println(matcher.find());
        // System.out.println(matcher.group(1)); // 文件名
        // ![文件名](assets/文件名)

        String standardFormat = "![文件名](assets/文件名)";

        InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
        BufferedReader bufReader = new BufferedReader(reader);

        String line = null;
        while ((line = bufReader.readLine()) != null) {
            Matcher matcher = p.matcher(line);

            if (matcher.find()) {
                String fileName = matcher.group(2);
                String imgUrl = matcher.group(1);
                String standardFileName = standardFormat.replaceAll("文件名", fileName);

                // System.out.println(imgUrl);
                // System.out.println(fileName);
                // System.out.println(standardFileName);

                saveImgToLocal(imgUrl, fileName, file.getParent());
                raf.write(standardFileName.getBytes(Charset.defaultCharset()));

            } else {
                raf.write(line.getBytes(Charset.defaultCharset()));
            }

            raf.write("\r\n".getBytes(Charset.defaultCharset()));

        }

        raf.close();

    }

    public static void saveImgToLocal(String imgUrl, String fileName, String dir) {

        log.info("获取图片, imgUrl: {}, fileName: {}, dir", imgUrl, fileName, dir);

        try {
            Resource resource = template.getForObject(imgUrl, Resource.class);

            FileOutputStream fos = new FileOutputStream(dir + "\\assets\\" + fileName);

            StreamUtils.copy(resource.getInputStream(), fos);

            fos.close();
        } catch (Exception e) {
            log.error("获取图片失败, imgUrl: {}, fileName: {}, dir", imgUrl, fileName, dir);
        }
    }

}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值