英文:
How to override JXBrowser default downloads file path?
问题
我正在努力找出在JX Browser层面是否存在任何属性,用于为文件/图像提供默认下载路径位置。我看到在Java端我们有setPopupHandler
和setDownloadHandler
,但我只是想知道在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;
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论