首页 > 编程语言 >Java获取URL中的参数 字符串截取

Java获取URL中的参数 字符串截取

时间:2022-10-26 11:12:53浏览次数:39  
标签:Java URL 截取 System fileName fileKey file println StringUtils

// 测试url
        String httpUrl = "https://www.baidu.com/rest/file-system/operation/download?fileKey=$55d7e9fd-3287-4499-9d9e-5cd52f593e4f$3236802050&signature=TTH1A8hWiX7g6gnh2OYRTOkRjzk%3D%0A&expire=1669292158061&type=file&fileName=%E5%B9%B3%E5%8F%B0%E7%BB%B4%E5%BA%A6%E5%AF%B9%E8%B4%A6%E5%8D%95";

        String decodeUrl = URLDecoder.decode(httpUrl, CharsetUtil.UTF_8);

        URL url = new URL(decodeUrl);
        System.out.println("URL 是 " + decodeUrl); // https://www.baidu.com/rest/file-system/operation/download?fileKey=$55d7e9fd-3287-4499-9d9e-5cd52f593e4f$3236802050&signature=TTH1A8hWiX7g6gnh2OYRTOkRjzk=&expire=1669292158061&type=file&fileName=平台维度对账单
        System.out.println("协议是 " + url.getProtocol());// https
        System.out.println("文件名是 " + url.getFile());// /rest/file-system/operation/download?fileKey=$55d7e9fd-3287-4499-9d9e-5cd52f593e4f$3236802050&signature=TTH1A8hWiX7g6gnh2OYRTOkRjzk=&expire=1669292158061&type=file&fileName=平台维度对账单
        System.out.println("主机是 " + url.getHost());// www.baidu.com
        System.out.println("路径是 " + url.getPath());// /rest/file-system/operation/download
        System.out.println("端口号是 " + url.getPort());// -1
        System.out.println("默认端口号是 " + url.getDefaultPort()); // 443

        String query = url.getFile();
//        拿到 ?fileKey= 后所有的值  $55d7e9fd-3287-4499-9d9e-5cd52f593e4f$3236802050&signature=TTH1A8hWiX7g6gnh2OYRTOkRjzk=&expire=1669292158061&type=file&fileName=平台维度对账单
        String s = org.apache.commons.lang3.StringUtils.substringAfter(query, "?fileKey=");
//        获取&signature= 前面的值  $55d7e9fd-3287-4499-9d9e-5cd52f593e4f$3236802050
        String fileKey = StringUtils.substringBefore(s, "&signature=");
//      可以直接拿到 $55d7e9fd-3287-4499-9d9e-5cd52f593e4f$3236802050
        String s2 = StringUtils.substringBetween(query, "?fileKey=", "&signature=");
//        获取&signature= 后面的值  O221018123614166496
        String fileName = org.apache.commons.lang3.StringUtils.substringAfter(query, "&fileName=");
        System.out.println("获取到 fileKey:  " + fileKey + "获取到 fileName:  " + fileName);
//        拼接新的地址  https://www.baidu.com/rest/file-system/operation/download?fileKey=$55d7e9fd-3287-4499-9d9e-5cd52f593e4f$3236802050&fileName=平台维度对账单
        String query1 = "https://www.baidu.com/rest/file-system/operation/download" + "?fileKey=" + fileKey + "&fileName=" + fileName;
        System.out.println("拼接的新地址 :" + query1);

//        截取某个字符串之前的字符
        StringUtils.substringBefore("hello world", "l");
//        结果是:he          这里是以第一个”l”,为标准。
        StringUtils.substringBeforeLast("hello world", "l");
//        结果为:hello wor   这里以最后一个“l”为准。

//        截取某个字符串之后的字符
        StringUtils.substringAfter("hello world", "l");
//        结果是:lo world   这里是以第一个”l”,为标准。
        StringUtils.substringAfterLast("hello world", "l");
//        结果为:d          这里以最后一个“l”为准。

//        截取两个字符串之间隔的字符
        StringUtils.substringBetween("hello world", "o");
//        结果是: w   两个o之间的字符串。
        StringUtils.substringBetween("hello world", "l", "r");
//        结果是: lo wo   第一个字符“l”与第一个字符“r”之间的字符串
        StringUtils.substringsBetween("hello world", "l", "r");

标签:Java,URL,截取,System,fileName,fileKey,file,println,StringUtils
From: https://www.cnblogs.com/fuqian/p/16827546.html

相关文章