Typora导出html文件图片自动转换成base64

Typora导出html文件图片自动转换成base64

一、出现问题

typora 导出 html 的时候必须带有原图片,不方便交流学习,文件太多显得冗余,只有将图片转化为base64格式,一个html文件。

本文档下载链接:https://www.lanzv.com/i0XYv1myf3ne 密码:bay7

Jar包下载:https://www.lanzv.com/iekaA1myf3ja 密码:444r

Java代码下载:https://www.lanzv.com/iPon51myf3kb 密码:a1y5

typora最新版本及激活码:https://www.lanzv.com/b05evh1aj 密码:3jaa

二、解决方案

  • 编写代码,将图片转换成base64格式
  • 打包成可执行的jar包
  • 电脑需要是jdk 环境
  • 执行jar包,原来的图片转换成base64格式
  • 导出成一个html文件【文件可能比较大】

三、编码实现

3.1.创建Java项目




3.2.代码

本文档下载链接:https://www.lanzv.com/i0XYv1myf3ne 密码:bay7

Jar包下载:https://www.lanzv.com/iekaA1myf3ja 密码:444r

Java代码下载:https://www.lanzv.com/iPon51myf3kb 密码:a1y5

java 复制代码
package com.xiaoxiao.util;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class TyporaImageToBase64 {
    /**
     * @param src img src 内容
     * @param end 下次查找字符串起始位置
     * @return java.lang.String
     * @throws
     * @description 递归执行查找同一行字符串多个 img 标签
     */
    public static String execute(String src, int end) {
        String result = matchImg(src, end);
        if (result.isEmpty()) {
            return src;
        } else {
            String[] split = result.split(",");
            String s1 = fileToBase64(split[0]);
            if (s1.isEmpty()) {
                return src;
            } else {
                String replace = src.replace(split[0], s1);
                return execute(replace, Integer.valueOf(split[1]) + 20);
            }
        }
    }

    /**
     * @param str 原始字符串
     * @return java.lang.String
     * @Description 匹配 img src 内容
     **/
    public static String matchImg(String str, int start) {
        int img = str.indexOf("<img", start); // 起始位置
        if (img == -1) {
            return "";
        }
        int l = str.indexOf("\"", img) + 1; // src 左侧 双引号
        int r = str.indexOf("\"", l); // src 右侧 双引号
        String substring = str.substring(l, r);
        if (substring.startsWith("data")) { // 跳过已经 base64 编码的文件 和 http 地址
            return matchImg(str, r);
        }
        return substring + "," + r; // src 地址 返回 src 内容以及最后的位置 使用逗号拼接
    }

    /**
     * @param path 文件路径
     * @return java.lang.String
     * @Description 文件转 base64
     **/
    public static String fileToBase64(String path) {
        File file = new File(path);
        if (!file.exists()) {
            System.err.printf("File not exist!");
            return "";
        }
        byte bytes[] = null;
        try (FileInputStream fileInputStream = new FileInputStream(path);) {
            bytes = new byte[fileInputStream.available()];
            fileInputStream.read(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Image convert base64 fail!");
        }
        // 文件后缀处理
        String suffix = getSuffix(path);
        return "data:image/" + suffix + ";base64," + Base64.getEncoder().encodeToString(bytes);
    }

    /**
     * @param str
     * @return java.lang.String
     * @throws
     * @description 获取文件后缀
     */
    public static String getSuffix(String str) {
        return str.substring(str.lastIndexOf(".") + 1);
    }

    // 主方法
    public static void main(String[] args) {
        // 获取文件路径
        if (args.length == 0) {
            System.out.println("No parameters passed");
            return;
        }
        String arg = args[0];
        // 获取文件后缀
        String suffix = getSuffix(arg);
        File srcFile = new File(arg);

        File outFile = new File(arg.replace("." + suffix, "").concat("-base64.").concat(suffix));
        try (BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), StandardCharsets.UTF_8));
             BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),StandardCharsets.UTF_8))
        ) {
            String len = "";
            while ((len = bfr.readLine()) != null) {
                String result = "";
                if (len.indexOf("<img") != -1) {
                    result = execute(len, 0);
                }
                if (result.equals("")) {
                    bfw.write(len);
                } else {
                    bfw.write(result);
                }
            }
            //必须关闭IO流,否则无法操作文件
            bfr.close();
            bfw.close();
            //成功,删除转换成功的源文件
            boolean resultDelete = srcFile.delete();
            System.out.println("Sorce file delete finish, " + resultDelete);
            boolean reultRename = outFile.renameTo(srcFile);
            System.out.println("Out file rename finish, " + reultRename);
            System.out.println("File convert success!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("error");
            System.out.println("File convert fail!");
        }

    }
}

3.3.打包成Jar包






四、如何使用

找到自己的jar包路径

java 复制代码
java -jar 【TyporaImageToBase64.jar路径】 "${outputPath}"


java -jar D:\JAVA\Typora\TyporaImageToBase64.jar "${outputPath}"



endl

相关推荐
西农小陈1 天前
Python-基于PyQt5,wordcloud,pillow,numpy,os,sys的智能词云生成器
开发语言·python·小程序·pycharm·numpy·pyqt·pillow
A.sir啊1 天前
爬虫基础(五)爬虫基本原理
网络·爬虫·python·网络协议·http·pycharm
张biubiu2 天前
vscode和pycharm的区别
ide·vscode·pycharm
西农小陈2 天前
Python-基于PyQt5,json和playsound的通用闹钟
开发语言·windows·python·pycharm·pyqt
红虾程序员4 天前
HTML一般标签和自闭合标签介绍
前端·pycharm·html·intellij-idea·html5
max5006005 天前
PyCharm介绍
ide·python·pycharm
人生无根蒂,飘如陌上尘5 天前
pycharm(2)
ide·python·pycharm
G.E.N.5 天前
PyCharm接入DeepSeek实现AI编程
人工智能·深度学习·语言模型·chatgpt·pycharm·ai编程
bksheng6 天前
【PyCharm】将包含多个参数的 shell 脚本配置到执行文件来调试 Python 程序
ide·python·pycharm
Serendipity_Carl6 天前
爬取NBA球员信息并可视化小白入门
爬虫·python·pycharm·数据分析·数据可视化