要从Java执行PHP脚本,您可以使用以下方法之一:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JavaExecutePHP {
public static void main(String[] args) {
try {
ProcessBuilder processBuilder = new ProcessBuilder("php", "your_php_script.php");
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class JavaExecutePHP {
public static void main(String[] args) {
try {
ProcessBuilder processBuilder = new ProcessBuilder("php", "your_php_script.php");
Process process = processBuilder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
process.waitFor();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class JavaExecutePHP {
public static void main(String[] args) {
try {
URL url = new URL("http://your_php_script_url.php");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
请注意,这些方法仅适用于本地和局域网环境中的PHP脚本。如果您需要从Java应用程序访问远程PHP脚本,请确保您的应用程序具有访问远程服务器的权限,并且远程服务器已正确配置以接受跨域请求。
领取专属 10元无门槛券
手把手带您无忧上云