2017-03-17 167 views
0

我想從java代碼執行curl命令。我已經看到幾個文件,關於相同的計算器問題。但它不是得到所要的結果,我試圖運行這個curl命令:從java代碼執行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命令。請幫忙。

+1

在命令行上的空間意味着一個新的說法。 –

+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())); 
    } 
0

您對如何將命令行轉換爲字符串參數列表的理解略有不正確。 (這大致相當於Unix shell的工作原理)。

通常,當命令行中存在空格時,意味着啓動一個新參數。所以

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

應該

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