// 测试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");
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?