java自定义URLStreamHandlerFactory

最近使用layui作为javafx的表现层,发现layui的字体文件在打包后无法正常加载,在经过仔细排查后,发现是打包后路径发生变化导致的,所以就自定义了URLStreamHandlerFactory来处理无法加载的文件。

static {
    URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory());
}

public static class CustomURLStreamHandlerFactory implements URLStreamHandlerFactory {
    @Override
    public URLStreamHandler createURLStreamHandler(String protocol) {
        if("local".equals(protocol)){
            return new LocalStreamHandler();
        }
        if ("jar".equals(protocol)){
            return new CustomJarStreamHandler();
        } else {
            return null;
        }
    }
}
public static class LocalStreamHandler extends URLStreamHandler {

    @Override
    protected URLConnection openConnection(URL u) throws IOException {
        return new CustomURLConnection(u);
    }
}
public static class CustomJarStreamHandler extends sun.net.www.protocol.jar.Handler {
    @Override
    protected URLConnection openConnection(URL url) throws IOException {
        if(url.toString().contains("local")||url.toString().contains("app.jar!")){
            return new CustomURLConnection(url);
        }else{
            return super.openConnection(url);
        }
    }
}

 

public static class CustomURLConnection extends HttpURLConnection {

    private InputStream inputStream;

    CustomURLConnection(URL url) throws IOException {
        super(url);
    }

    @Override
    public void disconnect() {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                log.debug(e.getMessage(), e);
            }
        }
        connected = false;
    }

    @Override
    public boolean usingProxy() {
        return false;
    }

    @Override
    public void connect() throws IOException {
        if (connected) {
            return;
        }
        inputStream = loadData(CustomURLConnection.class.getClassLoader());
        connected = true;
    }

    @Override
    public int getResponseCode() throws IOException {
        return HttpURLConnection.HTTP_OK;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return inputStream;
    }

    private InputStream loadData(ClassLoader loader) {
        String protocol = url.getProtocol();
        if (protocol.equals("jar")) {
            String path = url.getPath();
            if (path.contains("local")) {
                byte[] response = JavaScriptNative.scriptNative.getResponse(url.toExternalForm());
                return new ByteArrayInputStream(response);
            }
            if (path.contains("jar://file/")) {
                path = path.substring(path.indexOf("jar://file") + "jar://file".length() + 1);
                try {
                    path = URLDecoder.decode(path, "UTF-8");
                    return Files.newInputStream(Paths.get(path));
                } catch (IOException e) {
                    log.debug(e.getMessage(), e);
                }
                return new ByteArrayInputStream(new byte[0]);
            }
            path = path.substring(path.indexOf("app.jar!") + "app.jar!".length() + 1);
            return loader.getResourceAsStream(path);
        }
        byte[] response = JavaScriptNative.scriptNative.getResponse(url.toExternalForm());
        return new ByteArrayInputStream(response);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值