Java获取链接中的主域名[利用tld.min.js]

获取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文件中,代码再调用使用。
javascripturlhost代码如下:

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就不上传了,代码就上面那点的,将他们放到resourcesstatic目录,或者你可以另外的目录也行,记得改改代码中的路径即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值