Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用<;从webapps/webcontext/deploy文件夹外部加载图像;h:graphicImage>;或<;img>;标签_Image_Jsf_Jsf 2_Graphicimage - Fatal编程技术网

使用<;从webapps/webcontext/deploy文件夹外部加载图像;h:graphicImage>;或<;img>;标签

使用<;从webapps/webcontext/deploy文件夹外部加载图像;h:graphicImage>;或<;img>;标签,image,jsf,jsf-2,graphicimage,Image,Jsf,Jsf 2,Graphicimage,我需要使用JSF标记或HTML标记显示web应用程序中deploy文件夹之外的图像。我怎样才能做到这一点呢?关键是,它必须可以通过公共URL访问。因此,最终必须引用http://URI,而不是类似于文件://URI之类的东西。最终,HTML源代码在最终用户的机器上执行,图像在解析HTML源代码期间由webbrowser单独下载。当webbrowser遇到文件://URI,如C:\path\to\image.png,它将在最终用户自己的本地磁盘文件系统中查找映像,而不是在Web服务器上查找映像。如

我需要使用JSF
标记或HTML
标记显示web应用程序中deploy文件夹之外的图像。我怎样才能做到这一点呢?

关键是,它必须可以通过公共URL访问。因此,
最终必须引用
http://
URI,而不是类似于
文件://
URI之类的东西。最终,HTML源代码在最终用户的机器上执行,图像在解析HTML源代码期间由webbrowser单独下载。当webbrowser遇到
文件://
URI,如
C:\path\to\image.png
,它将在最终用户自己的本地磁盘文件系统中查找映像,而不是在Web服务器上查找映像。如果webbrowser在物理上与Web服务器不同的机器上运行,这显然是行不通的

有几种方法可以实现这一点:

  • 如果您完全控制images文件夹,则只需将包含所有图像的文件夹直接放入servletcontainer的deploy文件夹中,例如Tomcat的
    /webapps
    文件夹和GlassFish的
    /domains/domain1/applications
    文件夹。无需进一步配置


  • 或者,向服务器添加新的webapp上下文,该上下文指向包含这些映像的文件夹的绝对磁盘文件系统位置。如何做到这一点取决于所使用的容器。下面的示例假定图像位于
    /path/to/images
    中,并且您希望通过访问它们

    对于Tomcat,将以下新条目添加到Tomcat的
    /conf/server.xml
    内部

    对于WildFly,在
    /standalone/configuration/standalone.xml的
    中添加以下条目

    <location name="/images" handler="images-content" />
    
    如果您碰巧使用OmniFaces,则可能会很有用,因为它还考虑了头、缓存和范围请求


  • 或者,使用支持返回
    byte[]
    InputStream
    的bean属性的:

    @Named
    @ApplicationScoped
    public class Bean {
    
        public InputStream getImage(String filename) {
            return new FileInputStream(new File("/path/to/images", filename));
        }
    }
    

  • 或者,使用支持返回PrimeFaces-specific
    StreamedContent
    的bean方法的

    @Named
    @ApplicationScoped
    public class Bean {
    
        public StreamedContent getImage() throws IOException {
            FacesContext context = FacesContext.getCurrentInstance();
    
            if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
                // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
                return new DefaultStreamedContent();
            }
            else {
                // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
                String filename = context.getExternalContext().getRequestParameterMap().get("filename");
                return new DefaultStreamedContent(new FileInputStream(new File("/path/to/images", filename)));
            }
        }
    }
    

  • 对于第一种方法和第二种方法中的Tomcat和WildFly方法,图像将通过普通HTML可用,因此可以参考,如下所示

    <img src="/images/filename.ext" />
    
    <img src="#{request.contextPath}/images/filename.ext" />
    
    <o:graphicImage value="#{bean.getImage('filename.ext')}" />
    
    
    
    对于第二种方式和第三种方式中的GlassFish方法,图像将通过普通HTML可用,因此可以参考,如下所示

    <img src="/images/filename.ext" />
    
    <img src="#{request.contextPath}/images/filename.ext" />
    
    <o:graphicImage value="#{bean.getImage('filename.ext')}" />
    
    
    
    或者在JSF中,如下所示(上下文路径自动添加前缀)

    
    
    对于第四种方式中的OmniFaces方法,参考如下

    <img src="/images/filename.ext" />
    
    <img src="#{request.contextPath}/images/filename.ext" />
    
    <o:graphicImage value="#{bean.getImage('filename.ext')}" />
    
    
    
    对于第五种方式的PrimeFaces方法,参考如下:

    <p:graphicImage value="#{bean.image}">
        <f:param name="filename" value="filename.ext" />
    </p:graphicImage>
    
    
    
    请注意,示例
    {bean}
    @ApplicationScoped
    ,因为它基本上表示无状态服务。您还可以将其设置为
    @requestscope
    ,但随后将在每个请求上重新创建bean,而无需任何费用。您不能将其设置为
    @viewscope
    ,因为此时浏览器需要下载图像,服务器不会创建JSF页面。您可以将其设置为
    @SessionScoped
    ,但随后它会被免费保存在内存中

    另见:
    • (支持HTTP缓存)

    为了使用
    标记实现所需,您需要创建一个Tomcat v7别名,以便将外部路径映射到web应用的上下文

    要做到这一点,您需要。最简单的方法是定义包含以下内容的META-INF/context.xml文件:

    在JSP中

    “/>
    

    包是
    com.sun.jersey.core.util.Base64
    java.nio.file.path
    java.nio.file.Files
    ,在PrimeFaces中,您可以通过以下方式实现bean:

    <h:form>
      <h:commandLink id="downloadLink" value="Download">  
        <p:fileDownload value="#{fileDownloader.getStream(file.path)}" />  
    </h:commandLink>
    </h:form
    
    在HTML Facelet中:

    @ManagedBean
    @ApplicationScope
    public class FileDownloader {
    
        public StreamedContent getStream(String absPath) throws Exception {
            FileInputStream fis = new FileInputStream(absPath);
            BufferedInputStream bis = new BufferedInputStream(fis);
            StreamedContent content = new DefaultStreamedContent(bis);
            return content;
           }
        }
    }
    
    
    

    我建议设置属性cache=“false”“在graphicImage组件中。

    Hi BalusC,感谢您的快速回复。如果我在linux中使用它,我的映像绝对路径是/home/muneeswaran/apache-tomcat-6.0.29/headers/Aboutus/images/ss.jpg。那么我可以将上下文存储在server.xml中吗?是的,然后您可以通过访问它。但是,既然您似乎可以完全控制它(您已经将它放在Tomcat安装文件夹中!),为什么不将它放在/webapps文件夹中呢?那么你就不需要添加另一个上下文了。嗨,BalusC,我们已经使用servlet使用了一个名为emsd的上下文,我可以使用另一个名为images的上下文吗?如果我按照你的建议按工作方式使用它,它会将此上下文作为esmd的子上下文。因此,我无法使用访问图像。我该如何做?是的,你可以。用同样的方法访问它。@Harry:在asadmin中或通过
    sunweb.xml
    创建一个虚拟目录。我们不会用JSF而不是JSPHow,这在JSF中可能吗?
    <h:form>
      <h:commandLink id="downloadLink" value="Download">  
        <p:fileDownload value="#{fileDownloader.getStream(file.path)}" />  
    </h:commandLink>
    </h:form
    
    @ManagedBean
    @ApplicationScope
    public class FileDownloader {
    
        public StreamedContent getStream(String absPath) throws Exception {
            FileInputStream fis = new FileInputStream(absPath);
            BufferedInputStream bis = new BufferedInputStream(fis);
            StreamedContent content = new DefaultStreamedContent(bis);
            return content;
           }
        }
    }
    
    <img src="data:image/jpeg;base64,
    <%= new String(Base64.encode(Files.readAllBytes(Paths.get("C:\\temp\\A.jpg"))))%>"/>
    
    private StreamedContent image;
    
    public void setImage(StreamedContent image) {
        this.image = image;
    }
    
    public StreamedContent getImage() throws Exception {
        return image;
    }
    
    public void prepImage() throws Exception {
    File file = new File("/path/to/your/image.png");
    InputStream input = new FileInputStream(file);
    ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
    setImage(new DefaultStreamedContent(input,externalContext.getMimeType(file.getName()), file.getName()));
    }
    
    <body onload="#{yourBean.prepImage()}"></body> 
    <p:graphicImage value="#{youyBean.image}" style="width:100%;height:100%" cache="false" >
    </p:graphicImage>