下载

本指南介绍了如何管理文件下载、跟踪下载进度、在下载完成时获得通知等。

概述

要获取与特定 Profile 关联的所有下载的列表,包括在此应用程序会话期间已经完成的下载和活动下载,请使用以下方法:

List<Download> downloads = profile.downloads().list();
val downloads = profile.downloads().list()

使用 Engine.downloads() 您将获得默认配置文件的下载服务。

接受下载

每次 Browser 实例需要下载文件时,都会调用 StartDownloadCallback 回调。 在此回调中,您可以决定是否应该下载文件,还是必须取消下载请求。

browser.set(StartDownloadCallback.class, (params, tell) -> tell.cancel());
browser.set(StartDownloadCallback::class.java, StartDownloadCallback { params, tell -> tell.cancel() })

默认情况下,所有下载请求都会被取消。

要更改默认行为并允许下载文件,请使用以下方法:

browser.set(StartDownloadCallback.class, (params, tell) -> {
    Download download = params.download();
    // Download target details.
    DownloadTarget downloadTarget = download.target();
    // Tell the engine to download and save the file.
    tell.download(Paths.get(downloadTarget.suggestedFileName()));
});
browser.set(StartDownloadCallback::class.java, StartDownloadCallback { params, tell ->
    val download = params.download()
    // Download target details.
    val downloadTarget = download.target()
    // Tell the engine to download and save the file.
    tell.download(Path(downloadTarget.suggestedFileName()))
})

在上面的示例中,我们告诉库可以下载文件并将其保存到给定的目标文件中。

请确保 Chromium 主进程有权在给定目标目录中创建文件。 如果给定的目录已经有一个具有给定名称的文件,那么该文件将被自动覆盖。 我们建议您在接受文件下载之前检查权限和文件是否存在。

控制进度

要控制下载进度,请使用 Download 实例,您可以使用StartDownloadCallback.Params.download() 方法获取该实例。

例如:

browser.set(StartDownloadCallback.class, (params, tell) -> {
    Download download = params.download();
    ...
});
browser.set(StartDownloadCallback::class.java, StartDownloadCallback { params, tell ->
    val download = params.download()
    ...
})

暂停下载

要暂停下载,请使用 Download.pause() 方法:

download.pause();
download.pause()

恢复下载

要恢复暂停的下载,请使用 Download.resume() 方法:

if (download.isPaused()) {
    download.resume();
}
if (download.isPaused()) {
    download.resume()
}

取消下载

您可以随时取消下载,除非下载处于终止状态。 这包括已完成的下载、取消的下载和无法恢复的中断下载。

要确定下载是否处于终止状态,请使用 Download.isDone() 方法。

要取消下载,请使用 Download.cancel() 方法:

if (!download.isDone()) {
    download.cancel();
}
if (!download.isDone()) {
    download.cancel()
}

下载事件

您可以跟踪下载进度,在下载被取消、暂停、中断或完成时获得通知。

下载已取消

要在 Download 被取消时获得通知,请使用 DownloadCanceled 事件:

download.on(DownloadCanceled.class, event -> {});
download.on(DownloadCanceled::class.java) { event -> }

下载完成

要在 Download 完成时获得通知,请使用 DownloadFinished 事件:

download.on(DownloadFinished.class, event -> {});
download.on(DownloadFinished::class.java) { event -> }

下载暂停

要在 Download 暂停时获得通知,请使用 DownloadPaused 事件:

download.on(DownloadPaused.class, event -> {});
download.on(DownloadPaused::class.java) { event -> }

下载更新

要跟踪下载进度,请使用 DownloadUpdated 事件:

download.on(DownloadUpdated.class, event -> {
    // Print download progress in percents.
    event.progress().ifPresent(progress -> 
            System.out.println((int) (progress.value() * 100) + "%"));
    // The current download speed estimate in bytes/second.
    long currentSpeed = event.currentSpeed();
    // The total size of a file in bytes.
    long totalBytes = event.totalBytes();
    // The number or received (downloaded) bytes.
    long receivedBytes = event.receivedBytes();
});
download.on(DownloadUpdated::class.java) { event -> 
    // Print download progress in percents.
    event.progress().ifPresent { progress -> 
        println("${(progress.value() * 100).toInt()}%")
    }
    // The current download speed estimate in bytes/second.
    val currentSpeed = event.currentSpeed()
    // The total size of a file in bytes.
    val totalBytes = event.totalBytes()
    // The number or received (downloaded) bytes.
    val receivedBytes = event.receivedBytes()
}

下载中断

要在 Download 因某种原因中断时获得通知,请使用DownloadInterrupted 事件:

download.on(DownloadInterrupted.class, event -> {
    // The interrupt reason such as file access denied, network failed, etc.
    DownloadInterruptReason reason = event.reason();
});
download.on(DownloadInterrupted::class.java) { event ->
    // The interrupt reason such as file access denied, network failed, etc.
    val reason = event.reason()
}
Go Top