功能描述
无需引入三方依赖 文本匹配网址(支持多个) 网址解析(包括协议、主机、路径、参数等)
package com. qiangesoft. image. utils ;
import org. springframework. util. Assert ;
import org. springframework. util. CollectionUtils ;
import java. net. MalformedURLException ;
import java. net. URL ;
import java. util. ArrayList ;
import java. util. HashMap ;
import java. util. List ;
import java. util. Map ;
import java. util. function. Consumer ;
import java. util. regex. Matcher ;
import java. util. regex. Pattern ;
public class UrlHelper {
private static final Pattern PATTERN = Pattern . compile ( "((http|https|ftp):\\/\\/)?([\\w\\-]+\\.)+[\\w\\-]+(:[0-9]+)?(/[\\w\\[\\]\\-.~!*'();:@&=+$,/?#%]*)" ) ;
public static void main ( String [ ] args) throws MalformedURLException {
String content = "8.97 复制打开抖音,看看【三融在东帝汶的作品】找个没人的地方. 然后一直躺到下午 https://v.douyin.com:8087/i2eTcJ82/?id=111&name=fsfe g@O.kP NwS:/ 06/18 " ;
List < String > list = UrlHelper . findAll ( content) ;
for ( String s : list) {
System . out. println ( s) ;
}
String one = UrlHelper . findOne ( content) ;
System . out. println ( UrlHelper . getProtocol ( one) ) ;
System . out. println ( UrlHelper . getHost ( one) ) ;
System . out. println ( UrlHelper . getPort ( one) ) ;
System . out. println ( UrlHelper . getPath ( one) ) ;
System . out. println ( UrlHelper . getFile ( one) ) ;
System . out. println ( UrlHelper . getAuthority ( one) ) ;
System . out. println ( UrlHelper . getQuery ( one) ) ;
System . out. println ( UrlHelper . getQueryParam ( one) ) ;
}
public static String findOne ( String content) {
Assert . notNull ( content, "content must be not null !" ) ;
List < String > list = findAll ( content) ;
return CollectionUtils . isEmpty ( list) ? null : list. get ( 0 ) ;
}
public static List < String > findAll ( String content) {
Assert . notNull ( content, "content must be not null !" ) ;
List < String > list = new ArrayList < > ( ) ;
findAll ( content, ( matcher) -> {
list. add ( matcher. group ( 0 ) ) ;
} ) ;
return list;
}
private static void findAll ( String content, Consumer < Matcher > consumer) {
if ( null != content) {
Matcher matcher = PATTERN . matcher ( content) ;
while ( matcher. find ( ) ) {
consumer. accept ( matcher) ;
}
}
}
public static String getProtocol ( String url) throws MalformedURLException {
URL urlO = new URL ( url) ;
return urlO. getProtocol ( ) ;
}
public static String getAuthority ( String url) throws MalformedURLException {
URL urlO = new URL ( url) ;
return urlO. getAuthority ( ) ;
}
public static String getHost ( String url) throws MalformedURLException {
URL urlO = new URL ( url) ;
return urlO. getHost ( ) ;
}
public static int getPort ( String url) throws MalformedURLException {
URL urlO = new URL ( url) ;
return urlO. getPort ( ) == - 1 ? urlO. getDefaultPort ( ) : urlO. getPort ( ) ;
}
public static String getFile ( String url) throws MalformedURLException {
URL urlO = new URL ( url) ;
return urlO. getFile ( ) ;
}
public static String getPath ( String url) throws MalformedURLException {
URL urlO = new URL ( url) ;
return urlO. getPath ( ) ;
}
public static String getQuery ( String url) throws MalformedURLException {
URL urlO = new URL ( url) ;
return urlO. getQuery ( ) ;
}
private static Map < String , String > getQueryParam ( String url) {
String regex = "(\\?|&+)(.+?)=([^&]*)" ;
Pattern pattern = Pattern . compile ( regex) ;
Matcher matcher = pattern. matcher ( url) ;
Map < String , String > paramMap = new HashMap < > ( ) ;
while ( matcher. find ( ) ) {
String key = matcher. group ( 2 ) ;
String value = matcher. group ( 3 ) ;
paramMap. put ( key, value) ;
}
return paramMap;
}
}