简体   繁体   English

下载远程jar文件并监视下载进度

[英]Download remote jar files and monitor the download progress

I'm new to Java (I come from an ActionScript background) and I'd like to know if there's a way to download and monitor the download progress of remote jar files inside an applet, then use the classes and other resources that are inside the downloaded jar. 我是Java的新手(我来自ActionScript背景),我想知道是否有一种方法可以下载和监视applet中远程jar文件的下载进度,然后使用其中的类和其他资源下载的jar。 I know there's a JarURLConnection class, but I could not find a way to monitor the download progress through this class. 我知道有一个JarURLConnection类,但找不到通过此类监控下载进度的方法。

The reason why I need to do this is because I'm trying to make a JavaFX applet divided into modules, downloadable on demand. 之所以需要这样做,是因为我试图将JavaFX applet分为模块,可以按需下载。

The default behaviour downloading all Jar files is usually acceptable, it downloads and shows a progress bar, but it sounds like you have a requirement to split things up. 通常可以接受下载所有Jar文件的默认行为,它会下载并显示进度条,但这听起来像您需要拆分内容。 AFAIK, manually downloading classes and adding to a classpath (or custom class loaders) can't be done without signing your applet which is a path I guess you don't want to follow. AFAIK,手动下载类并添加到类路径(或自定义类加载器)中,必须在未签名小程序的情况下完成,这是我想您不希望遵循的路径。

You might want to take a look at lazy downloading , available since Java 1.6 update 10. 您可能想看一下懒惰下载 ,它从Java 1.6 update 10开始提供。

Without knowing your exact requirements, my general advice on how you could split it would be to download all class files in the initial Jar and download resources (images / sounds / properties) as required. 在不知道您的确切要求的情况下,关于如何拆分的一般建议是在初始Jar中下载所有类文件,然后根据需要下载资源(图像/声音/属性)。

Code for how to download from within an applet: 如何从小程序中下载代码:

    URL url = new URL(getCodeBase(), filename);
    URLConnection urlConnection = url.openConnection();
    BufferedInputStream bufferedInputStream = null;
    try {
        bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());
        // ... input data
    } finally {
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
    }

You could define a custom ClassLoader and use this for your remote modules. 您可以定义一个自定义的ClassLoader并将其用于您的远程模块。 This will let you control exactly how the class bytes are transferred to your applet. 这将使您可以精确控制类字节如何传输到小程序。 If you want to load entire JAR files at a time, you could make your custom class loader download JAR files when needed and unpack them with JarInputStream . 如果要一次加载整个JAR文件,则可以使自定义类加载器在需要时下载JAR文件,并使用JarInputStream解压缩它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM