【问题标题】:JSF Primefaces download fileJSF Primefaces 下载文件
【发布时间】:2015-09-02 22:42:28
【问题描述】:

我目前正在研究 JSF PrimeFaces,我想下载我的项目名称“CV”文件夹中存在的文件,但这里面临的问题是下面的代码

文件上传:

<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>

文件下载:

<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>

这是控制器类:

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;
    }
}

【问题讨论】:

  • 您“面临问题”。告诉我们,这些是什么。
  • 无法下载文件。无响应
  • 请检查服务器日志和浏览器中的错误控制台,然后说出您的情况。如果您在日志中发现错误,则对第二个没有任何兴趣。
  • 浏览器或服务器控制台未发现错误
  • 你说,你要下载一个文件……那你为什么要用文件uploader呢?

标签: java jsf primefaces


【解决方案1】:

您正试图从您的电脑而不是从服务器获取文件。由于应用程序在服务器上运行,因此根目录是您的服务器域根目录。 尝试将文件放入服务器并更改其目录。 例如,您可以在服务器的根目录中创建一个名为 filesToDownload 的文件夹并将您的文件放入其中。然后你应该会发现它是这样的:

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

我知道这个问题有点老了,但由于我面临同样的问题,我认为这个答案可能对其他人有所帮助。

【讨论】:

    【解决方案2】:

    您是否在页面中添加了该脚本;

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-31
      • 2012-08-23
      • 2018-02-03
      • 2012-05-18
      • 2013-07-23
      • 2012-04-04
      相关资源
      最近更新 更多