获取URL中的一级域名
如获取https://editor.csdn.net/md/?not_checkout=1
中的csdn.net
,由于有部分域名不是常用域名在用字符串操作来获取可能会出现不对的情况。
本次使用的是js来获取主域名。
tldjs.js
后端java 代码:
import cn.hutool.core.io.resource.ClassPathResource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @Description: 获取域名相关工具
* @Author raye
* @Date 2021/12/8
**/
public class HostUtil {
public static ScriptEngine engine ;
static {
FileReader reader =null;
FileReader reader1=null;
try {
ScriptEngineManager manager = new ScriptEngineManager();
//创建javascript引擎
engine = manager.getEngineByName("javascript");
//依次读取多个js文件
ClassPathResource resource = new ClassPathResource("classpath:"+File.separator+"static"+File.separator+"tld.min.js");
InputStream resourceStream = resource.getStream();
File file1 = File.createTempFile("tld.min",".js");//resource.getFile();
FileUtils.copyInputStreamToFile(resourceStream,file1);
reader1 = new FileReader(file1);
ClassPathResource resource1 = new ClassPathResource("classpath:"+File.separator+"static"+File.separator+"urlhost.js");
InputStream resource1Stream = resource1.getStream();
File file = File.createTempFile("urlhost",".js");
FileUtils.copyInputStreamToFile(resource1Stream,file);
reader = new FileReader(file);
engine.eval(reader1);
engine.eval(reader);
}catch (Exception e){
System.err.println("ScriptEngine 初始化失败:");
e.printStackTrace();
}finally {
try {
if(reader1 != null) {
reader1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 获取顶级域名
* @param host
* @return
*/
public static String getTopHost(String host){
String result=host;
if(engine instanceof Invocable) {
//调用函数,第一个是js的函数名,后边的是函数的参数
Invocable invoke = (Invocable)engine;
try {
result = (String)invoke.invokeFunction("getdomain", host);
} catch (ScriptException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 不带协议 端口
*
* @param link
* @return
*/
public static String getHost(String link) {
URL url;
String host = null;
try {
url = new URL(link);
host = url.getHost();
} catch (MalformedURLException e) {
}
return host;
}
/**
* 获取上一级域名
* @return
*/
public static String getParantHost(String host){
if(StringUtils.isBlank(host)){
return host;
}
String tophost = getTopHost(host);
if(host.equalsIgnoreCase(tophost)){
return host;
}
String parant =host.indexOf(".")>-1?host.substring(host.indexOf(".")+1):host;
return parant;
}
}
是使用javax.script
引擎来执行javascript
函数,貌似java也可以直接在代码中编写javascript代码,但是我没有成功,所以我采用的是将代码再度写在urlhost.js
文件中,代码再调用使用。
javascript
的urlhost
代码如下:
function getdomain(url) {
return this.tldjs.getDomain(url);
}
java代码引用就一个hutool-core
工具包。
js 2个文件:tld.min.js
和我自己的jsurlhost
tld.min.js
已上传
https://download.csdn.net/download/BovinLee/85561852
urlhost.js
就不上传了,代码就上面那点的,将他们放到resources
下static
目录,或者你可以另外的目录也行,记得改改代码中的路径即可。