获取urlclassloader加载的jar文件的大小

时间:2009-04-28 18:39:29

标签: jar classloader urlclassloader

有没有人知道找到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);

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您知道正在加载的网址是http网址,并且您可以确保他们所在的服务器返回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);
    }
}