侧边栏壁纸
  • 累计撰写 793 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

目 录CONTENT

文章目录

java中调用 命令行

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 183 阅读 / 215 字
温馨提示:
本文最后更新于 2022-07-23,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
只测试了window平台
package http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class testSimpleCURL {
    private static ProcessBuilder process = new ProcessBuilder();

    public static void main(String[] args) {

        String[] cmds = {"curl", "https://www.baidu.com"};//必须分开写,不能有空格
        for (int i = 0; i < 1000; i++) {
            cmds = "curl -X GET \"http://127.0.0.1:8090/api/admin/posts?more=true&admin_token=e8e7640bd61f46dfaa1027203e830a82\" -H \"accept: */*\" -H \"ADMIN-Authorization: e8e7640bd61f46dfaa1027203e830a82\"".split(" ");
            System.out.println(i);
            execCurl(cmds);
        }
    }

    //这里执行cmds命令
    public static void execCurl(String[] cmds) {
        process.command(cmds);
        Process p;
        try {
            p = process.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), StandardCharsets.UTF_8));
            String line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
						// 输出错误信息
						if (p.getErrorStream().available() > 0) {
                System.out.println("error : ");
                reader = new BufferedReader(new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8));
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            }
        } catch (IOException e) {
            System.out.print("error");
            e.printStackTrace();
        }
    }
}
0

评论区