将用户选择的 file/upload 文件下载到用户选择的目录,同时使用 primefaces
Download user selected file/upload a file to a user selected directory both with primefaces
我想下载用户 selected 的带有 primefaces 的文件。正如 "file Download" 的 primface 展示中所述,我能够针对特定文件执行此操作。但我真正想要的是,在按下 "download Button" 之后应该打开一个文件对话框,这样用户就可以 select 自己下载一个文件。这可能吗?
我当前的特定文件下载代码如下:
public void handleLanguageFileDownload() throws FileNotFoundException, IOException {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
File fileToDownload = new File(DataManager.configreader.getLang_source());
String fileName = fileToDownload.getName();
String contentType = ec.getMimeType(fileName);
int contentLength = (int) fileToDownload.length();
ec.responseReset();
ec.setResponseContentType(contentType);
ec.setResponseContentLength(contentLength);
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
OutputStream output = ec.getResponseOutputStream();
Files.copy(fileToDownload.toPath(), output);
fc.responseComplete();
}
我想要完全相同的文件上传行为,因此用户可以 select 自己上传文件的文件夹。我当前的实现仅将文件上传到特定文件夹。
我当前将文件上传到特定文件夹的代码如下所示:
public void handleLanguageFileUpload(FileUploadEvent event) throws IOException {
if (!this.role.canManageLanguage){
return;
}
String [] filePathParts = DataManager.configreader.getLang_source().split("/");
String uploadPathString = DataManager.configreader.getLang_source().replaceAll(filePathParts[filePathParts.length - 1],""); //event.getFile().getFileName()
File uploadPath = new File(uploadPathString);
File fileToUpload = new File(uploadPath, event.getFile().getFileName());
try (InputStream input = event.getFile().getInputstream()) {
if(event.getFile().getFileName().equals(filePathParts[filePathParts.length - 1])) { //fileToUpload.getName()
Files.copy(input, fileToUpload.toPath(), StandardCopyOption.REPLACE_EXISTING);
uiManager.userMessage.info (event.getFile().getFileName() + " " + this.translate("msg_has_been_uploaded") + " !");
this.getOwnTreeVersion();
}
else {
uiManager.userMessage.error (event.getFile().getFileName() + " " + this.translate("msg_is_wrong_cannot_be_uploaded") +": " + filePathParts[filePathParts.length - 1] + " !");
}
}
}
提前感谢您的帮助!!!
只有通过上传方法才能使用文件选择器,如果您需要添加 FileSizeLimite
和许多其他功能。
关于下载方法,我告诉过你这是不可能的,因为你有一个默认的文件位置(通常是下载)你可以在 Primefaces 网站上阅读更多相关信息 Primefaces Download File you can set it manually but it will not be dynamic look at this post 。
我想下载用户 selected 的带有 primefaces 的文件。正如 "file Download" 的 primface 展示中所述,我能够针对特定文件执行此操作。但我真正想要的是,在按下 "download Button" 之后应该打开一个文件对话框,这样用户就可以 select 自己下载一个文件。这可能吗?
我当前的特定文件下载代码如下:
public void handleLanguageFileDownload() throws FileNotFoundException, IOException {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
File fileToDownload = new File(DataManager.configreader.getLang_source());
String fileName = fileToDownload.getName();
String contentType = ec.getMimeType(fileName);
int contentLength = (int) fileToDownload.length();
ec.responseReset();
ec.setResponseContentType(contentType);
ec.setResponseContentLength(contentLength);
ec.setResponseHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
OutputStream output = ec.getResponseOutputStream();
Files.copy(fileToDownload.toPath(), output);
fc.responseComplete();
}
我想要完全相同的文件上传行为,因此用户可以 select 自己上传文件的文件夹。我当前的实现仅将文件上传到特定文件夹。
我当前将文件上传到特定文件夹的代码如下所示:
public void handleLanguageFileUpload(FileUploadEvent event) throws IOException {
if (!this.role.canManageLanguage){
return;
}
String [] filePathParts = DataManager.configreader.getLang_source().split("/");
String uploadPathString = DataManager.configreader.getLang_source().replaceAll(filePathParts[filePathParts.length - 1],""); //event.getFile().getFileName()
File uploadPath = new File(uploadPathString);
File fileToUpload = new File(uploadPath, event.getFile().getFileName());
try (InputStream input = event.getFile().getInputstream()) {
if(event.getFile().getFileName().equals(filePathParts[filePathParts.length - 1])) { //fileToUpload.getName()
Files.copy(input, fileToUpload.toPath(), StandardCopyOption.REPLACE_EXISTING);
uiManager.userMessage.info (event.getFile().getFileName() + " " + this.translate("msg_has_been_uploaded") + " !");
this.getOwnTreeVersion();
}
else {
uiManager.userMessage.error (event.getFile().getFileName() + " " + this.translate("msg_is_wrong_cannot_be_uploaded") +": " + filePathParts[filePathParts.length - 1] + " !");
}
}
}
提前感谢您的帮助!!!
只有通过上传方法才能使用文件选择器,如果您需要添加 FileSizeLimite
和许多其他功能。
关于下载方法,我告诉过你这是不可能的,因为你有一个默认的文件位置(通常是下载)你可以在 Primefaces 网站上阅读更多相关信息 Primefaces Download File you can set it manually but it will not be dynamic look at this post