如何覆盖JXBrowser的默认下载文件路径?

huangapple go评论66阅读模式
英文:

How to override JXBrowser default downloads file path?

问题

我正在努力找出在JX Browser层面是否存在任何属性,用于为文件/图像提供默认下载路径位置。我看到在Java端我们有setPopupHandlersetDownloadHandler,但我只是想知道在JX Browser层面是否存在任何属性,以便最终用户可以选择随时进行更改,就像Google Chrome的设置 -> 高级设置 -> 下载 -> 位置那样。

英文:

I am trying to figure out that any property exists at JX Browser level to provide default downloads path location for files/images. I see that we have setPopupHandler and setDownloadHandler in Java side but just wanted to know any property exists at JX Browser level so that end user can have a choice to change at anytime like google chrome Settings -> Advanced -> Downloads -> Location

答案1

得分: 1

覆盖默认下载文件路径需要获取其文件名,然后使用自定义文件路径和文件名创建新文件。最后,将默认的目标文件设置为该文件。如下所示:

browser.setDownloadHandler(new DownloadHandler() {
    public boolean allowDownload(DownloadItem download) {
        String fileName = FilenameUtils.getName(download.getDestinationFile().getAbsolutePath());
        //String ext = "." + FilenameUtils.getExtension(download.getDestinationFile().getAbsolutePath());
        String destination = "C:\\yourPath\\downloadLocation\\";
        File file = new File(destination + fileName);
        download.setDestinationFile(file);
        
        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;
    }
});
英文:

To override the default downloads file path, you'll need to get the filename of it, then create a new file with your custom file path + filename.
In the final, set the default destination file to that file. Like this.

	        browser.setDownloadHandler(new DownloadHandler() {
	            public boolean allowDownload(DownloadItem download) {
	            	String fileName = FilenameUtils.getName(download.getDestinationFile().getAbsolutePath());
	            	//String ext = "." + FilenameUtils.getExtension(download.getDestinationFile().getAbsolutePath());
	            	String destination = "C:\\yourPath\\downloadLocation\\";
	            	File file = new File(destination + fileName);
	            	download.setDestinationFile(file);
	            	
	                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;
	            }
	        });

huangapple
  • 本文由 发表于 2020年3月16日 00:51:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/60695337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定