java 如何判断 php 或者 https

java 如何判断 php 或者 https
用户647089807
昨天22:25
|
浏览 12

Java 可以通过检查 URL 协议头或文件扩展名来初步判断一个字符串是否指向 PHP 脚本或 HTTPS 链接。

判断是否为 HTTPS 链接

检查 URL 是否以 https:// 开头:

复制代码
String url = "https://example.com";
boolean isHttps = url.startsWith("https://");
System.out.println("Is HTTPS: " + isHttps); // 输出 true

判断是否为 PHP 文件路径/URL

通常通过检查文件扩展名 .php

复制代码
String path = "/api/user.php";
boolean isPhp = path.endsWith(".php");
System.out.println("Is PHP file: " + isPhp); // 输出 true

综合示例

复制代码
public class UrlChecker {
    public static void main(String[] args) {
        String input = "https://site.com/page.php";
        
        boolean isHttps = input.startsWith("https://");
        boolean isPhp = input.contains(".php"); // 或 endsWith(".php")
        
        System.out.println("HTTPS: " + isHttps);
        System.out.println("PHP: " + isPhp);
    }
}

注意:以上方法基于简单字符串匹配。对于复杂场景(如带查询参数的 URL),建议使用 java.net.URI 或正则表达式进行更精确的解析。

请通过【🔍追问按钮】发送完整错误截图

我要提问
复制内容
分享给好友
AI编程问答网 免责声明:
以上内容除特别注明外均来源于网友提问,AI编程问答网回答,权益归原著者所有;