从应用程序外部下载文件 [英] Downloading files from outside of the application

查看:221
本文介绍了从应用程序外部下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里的错误信息:


上下文路径:/ 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 :centerheaderText =Télécharger> 
< p:commandButton value =下载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;
}
}


解决方案

遇到一些情况来获取pdf文件,使用 java.net.URL#openStream()喜欢

  InputStream input = new URL( HTTP://本地主机:18080 / openCars /图像/ hichem.jpg).openStream(); 
file = new DefaultStreamedContent(stream,image / jpg,downloaded_optimus.jpg);


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

解决方案

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(stream, "image/jpg", "downloaded_optimus.jpg");

这篇关于从应用程序外部下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆