Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 下载用户选择的文件/使用primefaces将文件上载到用户选择的目录_Jsf_Primefaces_Jsf 2_Download_Upload - Fatal编程技术网

Jsf 下载用户选择的文件/使用primefaces将文件上载到用户选择的目录

Jsf 下载用户选择的文件/使用primefaces将文件上载到用户选择的目录,jsf,primefaces,jsf-2,download,upload,Jsf,Primefaces,Jsf 2,Download,Upload,我想下载用户选择的带有primefaces的文件。我可以为一个特定的文件这样做,如primface showcase“文件下载”中所述。但我真正想要的是,在按下“下载按钮”之后,应该会打开一个文件对话框,这样用户就可以为自己选择要下载的文件。可能吗 我当前的特定文件代码如下所示: public void handleLanguageFileDownload() throws FileNotFoundException, IOException { FacesContex

我想下载用户选择的带有primefaces的文件。我可以为一个特定的文件这样做,如primface showcase“文件下载”中所述。但我真正想要的是,在按下“下载按钮”之后,应该会打开一个文件对话框,这样用户就可以为自己选择要下载的文件。可能吗

我当前的特定文件代码如下所示:

    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(); 
}
    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] + " !");
            }
        }
    }
我希望文件上传的行为完全相同,因此用户可以自己选择要上传文件的文件夹。我当前的实现仅将文件上载到特定文件夹

我当前用于将文件上载到特定文件夹的代码如下所示:

    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(); 
}
    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网站上阅读更多关于它的信息。你可以手动设置,但它不会是动态的。看看这篇文章。

我做了类似的事情(给用户选择文件的机会)这就是你要找的吗?!?是的,这正是我想做的!如果你能帮我,那就太好了!谢谢这是一个非常复杂的解决方案,我知道stackoverflow并不是来给你一个解决方案的,我会试着向你解释如何使用
Spring
?!?不,我不跟斯普林一起工作,你看到了吗?!?嘿,是的,我刚刚看到了,非常感谢你的回复,但这对我来说太多了。我需要webb应用程序打开一个类似于JFileChooser的对话框,这样我就可以选择文件,选择文件并将它们保存到我选择的目录中,有没有更简单的方法呢?好的,看看这篇文章,告诉我你们需要什么,就像primefaces上传文件一样,是真的吗