0

これがシナリオです。html を作成してブラウザにエコーする php スクリプトがあります。html がエコーされる直前に、実行可能な Java jar ファイルを呼び出して html を渡し、それを処理して php に戻してエコーアウトできるようにする必要があります。

私が今思いつく唯一の方法は、htmlを一時ファイルに保存し、Javaにファイル名を渡し、それを変更してphpに再度ロードすることです。これは非常に遅くなる可能性があり、ファイルをディスクに書き込んでから、読み取り、変更、再度書き込み、再度読み取る必要があると思います。

私の質問は次のとおりです。特定のシナリオで、java に html ファイルを渡し、それを php に返すより高速な方法はありますか?

現在のスタック: Apache 2.4、PHP 5.4.7、Java 7、OS: Ubuntu

4

1 に答える 1

1

私は Marc B が提案した解決策を使用しました。この特定の状況の明確な例がオンラインにないため、将来誰かが必要になった場合に備えて、php と Java のコードを貼り付けます。

PHP コード:

<?php

$process_cmd = "java -jar test.jar";

$env = NULL;
$options = ['bypass_shell' => true];
$cwd = NULL;
$descriptorspec = [
    0 => ["pipe", "r"], // stdin is a pipe that the child will read from
    1 => ["pipe", "w"], // stdout is a pipe that the child will write to
    2 => ["pipe", "w"]  // stderr is a file to write to
];

$process = proc_open($process_cmd, $descriptorspec, $pipes, $cwd, $env, $options);

if (is_resource($process)) {

    //feeding text to java
    fwrite($pipes[0], "Test text");
    fclose($pipes[0]);

    //echoing returned text from java
    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    //It is important that you close any pipes before calling
    //proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "\n command returned $return_value\n";
}

?>

ジャワコード:

package Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {

    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String input;

        while ((input = br.readLine()) != null) {
            System.out.println(input + " java test string ");
        }

    }

}
于 2013-07-26T09:22:43.253 に答える