• java代码执行curl命令


    核心代码:

    public static String execCurl(String[] cmds) {
            ProcessBuilder process = new ProcessBuilder(cmds);
            Process p;
            try {
                p = process.start();
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                    builder.append(System.getProperty("line.separator"));
                }
                return builder.toString();
    
            } catch (IOException e) {
                System.out.print("error");
                e.printStackTrace();
            }
            return null;
        }

    测试用例:

    public static void main(String[] args) {
            String[] cmds = {"curl", "-X", "POST",
                    "http://localhost:9999/my/url?param1=1&param2=2",
                    "-H", "accept: */*", "-H", "Content-Type: application/json;charset=UTF-8", "-d"
                    , "{ \"bodyName\": \"bodyValue\"}"};
            System.out.println(execCurl(cmds));
        }

    注意命令符需要隔开,且不能有空格。

  • 相关阅读:
    mysql随手记
    Exception 和 RuntimeException区别
    数据结构与算法
    Idea快捷键
    eclipse快捷键
    了解Lua语言中的_index,newindex,rawget和rawset
    Cocos2d-x CCNotificationCenter 通知中心
    LUA-赋值语句
    CCOrbitCamera:create
    类型和值
  • 原文地址:https://www.cnblogs.com/miaoying/p/12426857.html
Copyright © 2020-2023  润新知