Downloading files from outside of the application [duplicate]

不问归期 提交于 2020-01-07 09:24:42

问题


I'm using jsf 2.2 Primefaces 6.0 and i already implemented a solution to download an images from the ressources file inside the application and it works fine but when i try to download from the outside an error message will appear.I need help to can download from the outside of the application:

Here the error message :

Context Path:/gestion-remboursement-web Servlet Path:/pages/listDemandes.jsf Path Info:null Query String:null Stack Trace 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)

Here the Xhtml code:

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

Here the java Bean code :

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

回答1:


Similar kind of situation faced to get pdf files, solved using java.net.URL#openStream() like

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


来源:https://stackoverflow.com/questions/44803952/downloading-files-from-outside-of-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!