【问题标题】:Execute CURL command from java code从java代码执行CURL命令
【发布时间】:2017-08-08 15:17:21
【问题描述】:

我想从 java 代码中执行 curl 命令。我已经看过几个文档,关于相同的stackoverflow问题。但它没有给出想要的结果,我正在尝试运行这个 curl 命令:

curl --noproxy <ip> 
-i 
-d "INSERT IN GRAPH <http://graph.com>{ <prop/Advanced_Data_Types> rdf:type owl:NamedIndividual ,
                                    <http://abcd/Video> ,
                                   <http://abcd/LearningResource/BookChapter> ;
                           <http://abcd/hasAuthor> <prop/Eric_Grimson> ;
                           <http://abcd/inLanguage> <http://abcd/#Hindi> ;
                           <http://abcd/sourceOrganization> <prop/IIT_Hyderabad> .}" 
-u "demo:demo" 
-H "Content-Type: application/sparql-query" http://<ip>:<port>/DAV/home/dba/xx

还有JAVA代码:

ProcessBuilder pb = new ProcessBuilder(
            "curl",
            "-i",
            "-d \"INSERT IN GRAPH <http://graph.com>{ <prop/Advanced_Data_Types> rdf:type owl:NamedIndividual ,<http://abcd/Video> ,<http://abcd/LearningResource/BookChapter> ;<http://abcd/hasAuthor> <prop/Eric_Grimson> ;<http://abcd/inLanguage> <http://abcd/#Hindi> ;<http://abcd/sourceOrganization> <prop/IIT_Hyderabad> .} ",
            "-u \"demo:demo\"",
            "-H \"Content-Type: application/sparql-query\"",
            "http://<ip>:<port>/DAV/home/dba/xx");

    pb.redirectErrorStream(true);
    Process p = pb.start();
    InputStream is = p.getInputStream();

    String line;
    BufferedInputStream bis = new BufferedInputStream(is);
    System.out.println(bis);

我的代码有什么问题吗?我想使用这个特殊的 curl 命令。请帮忙。

【问题讨论】:

  • 命令行中的空格表示新参数。
  • 谢谢。它有效。

标签: java curl


【解决方案1】:

您对如何将命令行转换为字符串参数列表的理解有些不正确。 (这大致相当于 Unix shell 的工作方式)。

通常,当命令行中出现空格时,它意味着开始一个新参数。所以

"-H \"Content-Type: application/sparql-query\""

应该是

"-H", "\"Content-Type: application/sparql-query\""

【讨论】:

    【解决方案2】:

    我就是这样在java中执行curl的。

        public static String invokeCurlGet(String _host, int _connectTimeout, int _maxTimeout, int _maxResLength, Charset _charset) throws IOException
        {
            byte[] res = execute("curl --connect-timeout " + _connectTimeout + " --max-time " + _maxTimeout + " -X GET " + _host, _maxResLength);
    
            return new String(res, _charset);
        }
    
        public static byte[] execute(String _cmd, int _maxResLength) throws IOException
        {
            Process process = Runtime.getRuntime().exec(_cmd);
    
            try
            {
                int result = process.waitFor();
                if(result != 0)
                {
                    throw new IOException("Fail to execute cammand. Exit Value[" + result + "], cmd => " + _cmd);
                }
            }
            catch(InterruptedException e)
            {
                process.destroyForcibly();
    
                throw new IOException(e);
            }
    
            BufferedInputStream in = null;
    
            try
            {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                in = new BufferedInputStream(process.getInputStream());
                byte[] buf = new byte[1024];
                int read = 0;
    
                while((read = in.read(buf)) != -1)
                {
                    out.write(buf, 0, read);
                    out.flush();
    
                    if(_maxResLength > 0 && out.size() > _maxResLength)
                    {
                        throw new IOException("Response length exceeded.");
                    }
                }
    
                return out.toByteArray();
            }
            finally
            {
                if(in != null)
                {
                    in.close();
                }
            }
        }
    
        public static void main(String[] args) throws Exception
        {
            System.out.println(invokeCurlGet("http://127.0.0.1:9000?messageId=aaaaaaaa&to=asdfsefwaf", 3, 10, 0, Charset.defaultCharset()));
    //      System.out.println(invokeCurlGet("https://github.com", 1, 1, 0, Charset.defaultCharset()));
        }
    

    【讨论】:

      猜你喜欢
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      相关资源
      最近更新 更多