Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)
JxBrowser provides API that allows handling all the file downloads. Using this API you can control what files should be downloaded, provide path to the destination directory where file must be saved and track download progress.(JxBrowser提供了允许处理所有文件下载的API。使用此API,您可以控制应下载哪些文件,提供必须保存文件的目标目录的路径以及跟踪下载进度。)
You can register your own implementation of the DownloadHandler interface to handle file downloads in your own way. The following sample demonstrates how to do this: (您可以注册自己的DownloadHandler接口实现,以自己的方式处理文件下载。下面的示例演示如何执行此操作:)
browser.setDownloadHandler(new DownloadHandler() { public boolean allowDownload(DownloadItem download) { download.addDownloadListener(new DownloadListener() { public void onDownloadUpdated(DownloadEvent event) { DownloadItem download = event.getDownloadItem(); if (download.isCompleted()) { System.out.println("Download is completed!"); } } }); System.out.println("Dest file: " + download.getDestinationFile().getAbsolutePath()); return true; } });
The DownloadItem class provides methods for pausing and canceling download process. See its corresponding methods.(DownloadItem类提供了用于暂停和取消下载过程的方法。参见其相应方法。)
The complete example that demonstrates how to register your own DownloadHandler implementation you can find below:(完整的示例演示了如何注册自己的DownloadHandler实现,您可以在下面找到:)
import com.teamdev.jxbrowser.chromium.Browser; import com.teamdev.jxbrowser.chromium.DownloadHandler; import com.teamdev.jxbrowser.chromium.DownloadItem; import com.teamdev.jxbrowser.chromium.events.DownloadEvent; import com.teamdev.jxbrowser.chromium.events.Download<span class="fr-marker" data-id="0" data-type="false" style="display: none; line-height: 0;"></span><span class="fr-marker" data-id="0" data-type="true" style="display: none; line-height: 0;"></span>Listener; import com.teamdev.jxbrowser.chromium.swing.BrowserView; import javax.swing.*; import java.awt.*; /** * The sample demonstrates how to handle file download. To cancel download * you must return {@code false} from the * {@link DownloadHandler#allowDownload(com.teamdev.jxbrowser.chromium.DownloadItem)} * method. To listed for download update events you can register your own * {@link com.teamdev.jxbrowser.chromium.events.DownloadListener}. */ public class DownloadSample { public static void main(String[] args) { Browser browser = new Browser(); BrowserView view = new BrowserView(browser); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.add(view, BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); browser.setDownloadHandler(new DownloadHandler() { public boolean allowDownload(DownloadItem download) { download.addDownloadListener(new DownloadListener() { public void onDownloadUpdated(DownloadEvent event) { DownloadItem download = event.getDownloadItem(); if (download.isCompleted()) { System.out.println("Download is completed!"); } } }); System.out.println("Destination file: " + download.getDestinationFile().getAbsolutePath()); return true; } }); browser.loadURL("ftp://ftp.teamdev.com/updates/jxbrowser-4.0-beta.zip"); } }