[英]cURL -o command won't output to file until I stop the java program executing the cURL command
我正在編寫一個Java程序,它創建一個cURL命令並執行它。 我沒有任何問題,除了一個命令不允許我將stdout流入我的程序。 stdout具有執行未來cURL命令所需的信息。
我一直在使用-o命令將stdout輸出到.txt文件,但除非我在執行cURL命令后立即停止Java程序,否則這不起作用。 我也嘗試了幾種不同的流媒體技術,但似乎都沒有。
不幸的是,我正在與之通信的API需要帳戶信息才能執行許多命令,但我將共享不顯示我的用戶名和密碼的代碼。
這是執行並返回將來命令所需的值的命令:
curl -o C:\Users\friza\eclipse-workspace\CanaryCoalMine\birdseed.txt -k -J -X POST -v --include -H "X-gts-token:xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" https://api.gts.geant.net/taas/api/v2/projects/CanaryCoalMine/types/OneHost/reservations
這是birdseed.txt文件的輸出。 最后的五位數值是我需要的:
HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 20 Jun 2019 16:25:07 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.google.com/recaptcha http://www.springframework.org http://java.sun.com/jsp https://www.google.com/jsapi https://www.google.com https://www.gstatic.com code.jquery.com ajax.googleapis.com https://*.bootstrapcdn.com https://*.cloudflare.com; img-src 'self' http://195.113.161.164:* https://www.gstatic.com/recaptcha code.jquery.com; style-src 'self' 'unsafe-inline' https://*.bootstrapcdn.com https://*.cloudflare.com https://fonts.googleapis.com code.jquery.com; font-src 'self' https://themes.googleusercontent.com https://*.bootstrapcdn.com https://*.cloudflare.com; frame-src 'self' 'unsafe-inline' https://www.google.com; object-src 'none'
Expect-CT: enforce; max-age=3600
Strict-Transport-Security: max-age=31622400; includeSubDomains; preload
Referrer-Policy: no-referrer
Feature-Policy: geolocation 'none';midi 'none';notifications 'none';push 'none';sync-xhr 'none';microphone 'none';camera 'none';magnetometer 'none';gyroscope 'none';speaker 'self';vibrate 'none';fullscreen 'self';payment 'none';
10940
這是我用來執行上面代碼的Java方法:
public static void createType(String tokenHeader, String dslEncoded) {
String curlCommand = "curl -o C:\\Users\\friza\\eclipse-workspace\\CanaryCoalMine\\birdseed.txt -k -J -X POST -v --include -H "+tokenHeader
+" -H \"Content-Type: application/x-www-form-urlencoded\" -d \"script="
+dslEncoded+
"\" https://api.gts.geant.net/taas/api/v2/projects/CanaryCoalMine/types";
try {
//execute Curl Command to create new type
Process process = Runtime.getRuntime().exec(curlCommand);
System.out.println("Creating Type...");
System.out.println("Sent: " + curlCommand);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IO EXCEPTION :( ");
e.printStackTrace();
}
}
TL; DR預期:執行cURL命令后,我希望在文件中看到給定的輸出而不停止執行命令的Java程序。
實際結果:在Java程序中按STOP之前,無法在.txt文件中看到上述輸出。 在我能夠從文件中提取最后五位數之前,無法繼續執行該程序。 由於某種原因未知,無法直接流式傳輸。
添加到user207421說...
它很可能是一個緩沖讀取問題。
處理外部進程非常困難,如果外部程序的緩沖區填滿,它將停止響應/工作,直到該緩沖區被清除。
因此,當從外部進程讀取緩沖區時,那些讀取進程必須在它們自己的線程中進行。
以下是我的團隊前一段時間處理外部流程的一些簡單示例。
主要方法......
package processbuilder;
import processbuilder.streamreader.PrintStreamStreamReader;
public class CurlProcessBuilder {
public static void main(String... args) throws Exception {
ProcessBuilder processBuilder = new ProcessBuilder("curl", "-v", "-s", "-k", "https://localhost:8443");
Process process = processBuilder.start();
PrintStreamStreamReader outReader = new PrintStreamStreamReader(System.out, process.getInputStream());
Thread outThread = new Thread(outReader);
outThread.start();
PrintStreamStreamReader errReader = new PrintStreamStreamReader(System.err, process.getErrorStream());
Thread errThread = new Thread(errReader);
errThread.start();
int exitValue = process.waitFor();
System.out.println("Exit value: " + exitValue);
}
}
用於從進程緩沖區讀取的線程...
package processbuilder.streamreader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
public class PrintStreamStreamReader implements Runnable {
private PrintStream printStream;
private InputStream inputStream;
public PrintStreamStreamReader(PrintStream printStream, InputStream inputStream) {
this.printStream = printStream;
this.inputStream = inputStream;
}
@Override
public void run() {
try {
int c;
byte[] data = new byte[1024];
while ((c = inputStream.read(data, 0, data.length)) != -1) {
printStream.write(data, 0, c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
處理長時間運行的外部過程是一件痛苦的事。 以下是一些解釋問題的其他資源。 我不得不多次閱讀所有這些鏈接,以弄清楚實際上說的是什么以及為什么讓一切“恰到好處”如此困難。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.