【问题标题】:Downloading files from outside of the application [duplicate]从应用程序外部下载文件[重复]
【发布时间】:2017-12-01 21:42:43
【问题描述】:

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

这里是错误信息:

上下文路径:/gestion-remboursement-web Servlet 路径:/pages/listDemandes.jsf 路径信息:空 查询字符串:空 堆栈跟踪 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;
    }
}

【问题讨论】:

  • 你要我的下载方法吗?!? (我保证它有效)
  • 是的,任何运营解决方案都能满足我的需求。

标签: java primefaces download jsf-2.2


【解决方案1】:

获取pdf文件时遇到的类似情况,使用java.net.URL#openStream()解决

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

【讨论】:

  • 请在您的回答中添加一些解释
猜你喜欢
  • 2020-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多