【问题标题】:Get size of jar file loaded by urlclassloader获取 urlclassloader 加载的 jar 文件的大小
【发布时间】:2010-10-22 09:47:59
【问题描述】:

有人知道找到由 urlclassloader 动态加载的文件大小的好方法吗?

我正在以下列方式使用 urlclassloader,但需要跟踪使用了多少带宽。

URLClassLoader sysloader = (URLClassLoader) ClassLoader
            .getSystemClassLoader();
Class<URLClassLoader> sysclass = URLClassLoader.class;
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, (Object[]) urls);

提前致谢!

【问题讨论】:

    标签: jar classloader urlclassloader


    【解决方案1】:

    如果您知道正在加载的 url 是 http url,并且您可以确保它们所在的服务器返回 Content-Length 标头,您可以通过以下方式获取它们的大小:

    public static int getContentLength (URL url)
        throws IOException
    {
        String contentLength = url.openConnection().getHeaderField("Content-Length");
        if (contentLength == null) {
            throw new IllegalArgumentException(url + " didn't have a "
                + "Content-Length header");
        } else {
            return Integer.parseInt(contentLength);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-08
      • 2011-06-07
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多