简体   繁体   English

JSF Primefaces下载文件

[英]JSF Primefaces download file

I am currently working on JSF PrimeFaces, I want to download file which is present in my project name "CV " folder but facing issues here is the code below 我目前正在使用JSF PrimeFaces,我想下载文件,该文件存在于我的项目名称“ CV”文件夹中,但此处遇到的问题是以下代码

File upload: 上传文件:

<h:form enctype="multipart/form-data">
    <p:fileUpload
        fileUploadListener="#{fileUploadController.handleFileUploadCv}"
        mode="advanced" update="messages"
        allowTypes="/(\.|\/)(doc|docx)$/" />
    <p:growl id="messages" showDetail="true" />
</h:form>

File download: 档案下载:

<h:form id="form11">
    <p:commandButton id="downloadLink" value="Download Cv" ajax="false"
        onclick="PrimeFaces.monitorDownload(start, stop)"
        icon="ui-icon-arrowthichk-s">
        <p:fileDownload value="#{fileDownloadController.file}" />
    </p:commandButton>
</h:form>

Here is the controller class: 这是控制器类:

import java.io.InputStream;  
import org.primefaces.model.DefaultStreamedContent;  
import org.primefaces.model.StreamedContent;  
@ManagedBean
@SessionScoped
public class FileDownloadController {  

    private StreamedContent file;  

    public FileDownloadController() {

        InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("D:/Final Year Project/displayjob-portlet/docroot/cv/Junaid.cv");  
        System.out.print("inside download111");
        file = new DefaultStreamedContent(stream);  
    }

    public StreamedContent getFile() {
        return file;
    }
}

You are trying to get a file from your pc and not from the server. 您正在尝试从PC而不是从服务器获取文件。 Since the application is running on a server, the root directory is your server domain root directory. 由于该应用程序在服务器上运行,因此根目录是服务器域的根目录。 Try to put the file inside the server and change the directory of it. 尝试将文件放入服务器中并更改其目录。 For example you can create a folder inside the root directory of server called filesToDownload and put your file inside it. 例如,您可以在服务器的根目录中创建一个名为filesToDownload的文件夹,并将文件放入其中。 Then you should find it like this: 然后,您应该会发现它像这样:

InputStream stream = ((ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext()).getResourceAsStream("filesToDownload/Junaid.cv");

I know that this issue is a bit old, but as I was facing same problem, I think this answer might help someone else. 我知道这个问题有点老,但是由于我面临着同样的问题,我认为这个答案可能会对其他人有所帮助。

Did you add that script in your page ; 您是否在页面中添加了该脚本?

<script type="text/javascript">
function start() {
    PF('statusDialog').show();
}

function stop() {
    PF('statusDialog').hide();
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM