从java代码执行CURL命令

时间:2017-03-17 07:02:47

标签: java curl

我想从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命令。请帮忙。

2 个答案:

答案 0 :(得分:0)

我这样用它来执行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()));
    }

答案 1 :(得分:0)

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

通常,当命令行中存在空格时,意味着启动新参数。所以

CREATE OR REPLACE FUNCTION knn_join
 RETURN join_jt
IS
   CURSOR cur_fv_table
   IS
      SELECT id, fv
        FROM londonfv
       WHERE id <= 3000;

   retval       join_jt := join_jt ();
   var_fv       londonfv.fv%TYPE;
   var_id       londonfv.id%TYPE;
   join_table   join_jt := join_jt ();

BEGIN
   OPEN cur_fv_table;

  LOOP
      --Fetching records of cursor to variable var_id & var_fv  
      FETCH cur_fv_table INTO var_id, var_fv;


      SELECT join_t (r.id, r.fv) -- You made mistake here. You need to select your table columns here not any variable.
        BULK COLLECT INTO join_table --- Populating the collection
        FROM londonfv r
       WHERE manhattan_dist (var_id, var_fv) <= 5; -- Checking from the function

      --- Assuming there is only 1 record in collection join_table. 
      retval.EXTEND;
      --- Storing the value of into the collection
      retval := join_table;

      /*  If there are more then 
         for rec in 1..join_table.count
         loop
          retval.EXTEND;
          retval(rec):= join_table(rec);              
        end loop; 
       */
    END LOOP;

   RETURN retval;
END;
/

应该是

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