从应用程序外部下载文件

时间:2017-06-28 13:30:09

标签: java primefaces download jsf-2.2

我正在使用jsf 2.2 Primefaces 6.0,我已经实现了从应用程序内的ressources文件下载图像的解决方案,它工作正常但是当我尝试从外部下载时会出现一条错误消息.I需要帮助才能从应用程序外部下载:

这里出现错误信息:

  

上下文路径:/ gestion-remboursement-web   Servlet路径:/pages/listDemandes.jsf   路径信息:null   查询字符串:null   堆栈跟踪   javax.servlet.ServletException   javax.faces.webapp.FacesServlet.service(FacesServlet.java:667)   io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)   io.undertow.servlet.handlers.FilterHandler $ FilterChainImpl.doFilter(FilterHandler.java:130)   filters.LoginFilter.doFilter(LoginFilter.java:44)

这里是Xhtml代码:

<p:column style="text-align: center" headerText="Télécharger">
                    <p:commandButton value="Download" ajax="false"
                        onclick="PrimeFaces.monitorDownload(start, stop);"
                        icon="ui-icon-arrowthick-1-s">
                        <p:fileDownload value="#{fileDownloadView.file}" />
                    <f:setPropertyActionListener value="#{a}"
                        target="#{demandeBean.demandeSelectionnee}" />
                </p:commandButton>
            </p:column>

这里是java Bean代码:

public class FileDownloadView {

    private StreamedContent file;

    public FileDownloadView() {
        InputStream stream = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(
                "http://localhost:18080/openCars/images/hichem.jpg");
        file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");
    }

    public StreamedContent getFile() {
        return file;
    }
}

2 个答案:

答案 0 :(得分:0)

解决方案在xhtml和managedBean之间分开

<强> MyXhtml

react-router@3.x

<强> MyMangedBean

<p:commandButton value="Download" ajax="false"
                        actionListner="myManagedBean.downloadMethod()"
                        icon="ui-icon-arrowthick-1-s">

最后是public void downloadMethod() throws Exception { File file = new File(YourPath); InputStream input = new FileInputStream(file); ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); setDownload(new DefaultStreamedContent(input, externalContext.getMimeType(file.getName()), file.getName())); } 方法:

setDownload

希望能帮到你。

答案 1 :(得分:0)

获取pdf文件面临类似的情况,使用java.net.URL#openStream()解决了

 InputStream input = new URL("http://localhost:18080/openCars/images/hichem.jpg").openStream();
file = new DefaultStreamedContent(stream, "image/jpg", "downloaded_optimus.jpg");