proto转java文件工具类

1.下载win proto工具类,链接如下

https://objects.githubusercontent.com/github-production-release-asset-2e65be/23357588/07f4719e-398c-4cf7-a0c9-761e2f395ae9?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A/20220116/us-east-1/s3/aws4_request&X-Amz-Date=20220116T032127Z&X-Amz-Expires=300&X-Amz-Signature=9f9b1842e58e73b52633125902fa30f4caf0bc53590097bca7dcdf4816314720&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=23357588&response-content-disposition=attachment; filename=protoc-3.19.3-win64.zip&response-content-type=application/octet-stream

import java.io.*;import java.util.ArrayList;import java.util.List;/** * .proto文件编译成Java文件 * */public class ProtoToJavaUtils {    /**     * 当前项目路径     */    private static String url = System.getProperty("user.dir");    private static final String PROJECT_NAME = "springboot-starter-im-master";    private static String proto_path = "\src\main\resources\proto";    /**     * 这里一般不需要带包名,因为.proto文件一般为指定包名并自动生成对应的包     */    private static String java_path = "\src\main\java\";    /**     * 如果只是想要编译部分(指定)proto文件     * <p>     * 子包下必须带路径,eg:\user\user.proto or \user.proto     */    private static final List<String> PROTO_FILES = new ArrayList<>();    /**     * false: 编译部分(指定)proto文件,配合PROTO_FILES     * <p>     * true: 编译 PROTO_PATH 路径下的所有.proto文件     */    private static boolean isFiles = true;    private static Runtime runtime = Runtime.getRuntime();    public static void main(String[] args) {        transform();    }    private static void transform() {        if (!url.endsWith(PROJECT_NAME)) {            url = url + "\" + PROJECT_NAME;        }        proto_path = url + proto_path;        java_path = url + java_path;        System.out.println("JAVA_PATH:" + java_path);        System.out.println("PROTO_PATH:" + proto_path);        System.out.println("============================File out put===================================");        String command = "";        try {            // 路径下全部            if (isFiles) {                File protoFile = new File(proto_path);                if (protoFile.exists()) {                    File[] files = protoFile.listFiles();                    for (File file : files) {                        command = getCommand(file.getName());                        if (!command.isEmpty()) {                            outPutJava(command);                        }                    }                }            } else {                // 指定.proto文件                if (PROTO_FILES.size() > 0) {                    for (String protoFile : PROTO_FILES) {                        File file = new File(proto_path + protoFile);                        if (!file.exists()) {                            System.err.println("files is not exist : " + proto_path);                            continue;                        }                        command = getCommand(file.getName());                        if (!command.isEmpty()) {                            outPutJava(command);                        }                    }                }            }        } catch (Exception e) {            e.printStackTrace();        }    }    /**     * 输出java文件     *     * @param command     * @throws IOException     */    private static void outPutJava(String command) throws IOException {        System.out.println(">>>:" + command);        // 执行        Process process = runtime.exec(command);        // 打印结果        InputStreamReader isr = new InputStreamReader(process.getErrorStream());        BufferedReader br = new BufferedReader(isr);        boolean isSuccess = true;        String line = null;        while ((line = br.readLine()) != null) {            System.err.println("ERROR:" + line);            isSuccess = false;        }        if (isSuccess) {            System.out.println(">>>:success");        }        br.close();        isr.close();    }    /**     * 获取CMD执行命令     *     * @param fileName     * @return     */    private static String getCommand(String fileName) {        StringBuffer command = new StringBuffer();        command.append("C:\Users\lyz\Downloads\protoc-3.19.3-win64\bin\protoc.exe")                .append(" --java_out=").append(java_path)                .append(" --proto_path=").append(proto_path)                .append(" ").append(fileName);        return command.toString();    }}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没事搞点事做serendipity

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值