Lütfen 2. güncelleştirmeye aşağı doğru hareket edin. Bu sorunun önceki bağlamını değiştirmek istemedim.InputStream, ProcessBuilder ürününe nasıl yollanır
Bir Java uygulamasından wkhtmltoimage kullanıyorum.
Kullanmanın standart yolu - path-to-exe http://url.com/ image.png
.
Dokümanlarına göre, bir giriş URL'si yerine -
yazarsak, giriş STDIN'e geçer.
Ben ProcessBuilder
kullanarak işlemi başlıyorum -
ProcessBuilder pb = new ProcessBuilder(exe_path, " - ", image_save_path);
Process process = pb.start();
Şimdi anlamaya edemiyorum nasıl bu sürece boru bir giriş akışı için.
BenDataInputStream
içine oku şablon dosyası var ve ben sonunda bir dize ekleyerek ediyorum:
DataInputStream dis = new DataInputStream (new FileInputStream (currentDirectory+"\\bin\\template.txt"));
byte[] datainBytes = new byte[dis.available()];
dis.readFully(datainBytes);
dis.close();
String content = new String(datainBytes, 0, datainBytes.length);
content+=" <body><div id='chartContainer'><small>Loading chart...</small></div></body></html>";
nasıl pipomu content
sürecin STDIN
için?
ben kullandım sürecin getOutputStream()
: Andrzej Doyle tarafından cevap takiben
GÜNCELLEME ---
ProcessBuilder pb = new ProcessBuilder(full_path, " - ", image_save_path);
pb.redirectErrorStream(true);
Process process = pb.start();
System.out.println("reading");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
bw.write(content);
Aksi belirten bir hata veriyor:
2 GÜNCELLEME --------
geçerli kod bloğu gibi geçerli:
try {
ProcessBuilder pb = new ProcessBuilder(full_path, "--crop-w", width, "--crop-h", height, " - ", image_save_path);
System.out.print(full_path+ "--crop-w"+ width+ "--crop-h"+ height+" "+ currentDirectory+"temp.html "+ image_save_path + " ");
pb.redirectErrorStream(true);
Process process = pb.start();
process.waitFor();
OutputStream stdin = process.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
// content is the string that I want to write to the process.
writer.write(content);
writer.newLine();
writer.flush();
writer.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
}
yukarıdaki kodu Koşu bana veren IOException: The pipe is being closed.
yapmam gereken başka ne
boruyu açık tutmak için?
Eğer program sonunda pb.start hareket olmamalı (Peter muhtemelen bu yüzden size söylerdim önerildiği gibi Aslında ... sürecin çıkışında bakarak)? (bw.write (content) 'dan sonra, sadece bir önsezi "başladıktan sonra bir işleme nasıl yazabiliriz?" e dayanıyor. – Sap
@Grrrrr Bence çıkış akışını elde etmek için bir işlem başlatmamız gerekecek. –
Çıktı akışına veri yazıyorsunuz, "işleminiz için veri yazıyorsunuz!" Eğer işlemin çıktısını almak istiyorsanız, o zaman process.getInputStream yapmalı ve sonra bunu okuyalım – Sap