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

发布于 2024-07-24 20:27:40 字数 194 浏览 3 评论 0原文

我是 Java 新手(我有 ActionScript 背景),我想知道是否有一种方法可以下载并监视小程序内远程 jar 文件的下载进度,然后使用其中的类和其他资源下载的罐子。 我知道有一个 JarURLConnection 类,但我找不到通过该类监视下载进度的方法。

我需要这样做的原因是因为我试图将 JavaFX 小程序分成多个模块,以便按需下载。

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. I know there's a JarURLConnection class, but I could not find a way to monitor the download progress through this class.

The reason why I need to do this is because I'm trying to make a JavaFX applet divided into modules, downloadable on demand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下客 2024-07-31 20:27:40

下载所有 Jar 文件的默认行为通常是可以接受的,它会下载并显示进度条,但听起来您需要将东西分开。 AFAIK,如果不签署您的小程序,则无法手动下载类并添加到类路径(或自定义类加载器),这是我猜您不想遵循的路径。

您可能想看看 延迟下载,自 Java 1.6 update 10 起可用。

在不知道您的确切要求的情况下,我对如何拆分它的一般建议是下载初始 Jar 中的所有类文件并根据需要下载资源(图像/声音/属性) 。

如何从小程序中下载的代码:

    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();
        }
    }

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. 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.

You might want to take a look at lazy downloading, available since 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.

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();
        }
    }
情绪操控生活 2024-07-31 20:27:40

您可以定义自定义 ClassLoader 并将其用于您的远程模块。 这将使您能够准确控制类字节如何传输到您的小程序。 如果您想一次加载整个 JAR 文件,您可以让自定义类加载器在需要时下载 JAR 文件,并使用 JarInputStream

You could define a custom ClassLoader and use this for your remote modules. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文