OpenCms 如何读取图片文件字节流

OpenCms 如何在模版里面读取图片文件字节流呢?

 

可以参考我下面这段代码:

首先是.xsd文件:

<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

    <xsd:include schemaLocation="opencms://opencms-xmlcontent.xsd"/>
        
    <xsd:element name="TestNewss" type="OpenCmsTestNewss"/>
    
    <xsd:complexType name="OpenCmsTestNewss">
        <xsd:sequence>
            <xsd:element name="TestNews" type="OpenCmsTestNews" minOccurs="0" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="OpenCmsTestNews">
        <xsd:sequence>
            <xsd:element name="Title" type="OpenCmsString" />
            <xsd:element name="Date" type="OpenCmsDateTime" />            
            <xsd:element name="Text" type="OpenCmsHtml" />
            <xsd:element name="Image" type="OpenCmsVfsFile" minOccurs="1" maxOccurs="3"/>
			<xsd:element name="Wap" type="OpenCmsBoolean"/>
        </xsd:sequence>
        <xsd:attribute name="language" type="OpenCmsLocale" use="required"/>
    </xsd:complexType>

    <xsd:annotation>
        <xsd:appinfo>
            <resourcebundle name="org.opencms.testnews.workplace"/>
            <preview uri="${previewtempfile}" />
			<!-- 验证开始,验证标题(Title)不能为空 -->
            <validationrules>
                <rule element="Title" regex="^.*$"  message="${key.editor.errormessage.TestNews.mustnotempty}"/>
            <!-- 验证结束 -->
            </validationrules>
            <mappings>
                <mapping element="Title" mapto="property:Title" />
            </mappings>
            <defaults>
                <default element="Date" value="${currenttime}"/>
            </defaults>
        </xsd:appinfo>

    </xsd:annotation>  
</xsd:schema>
 可以看到image不止一个,所以就要使用cms:contentloop和cms:contentshow来获得所有图片的地址。

 

<cms:contentload collector="singleFile" param="%(opencms.uri)" editable="true">

<!-- 循环获得所有图片的src -->
<cms:contentloop element="Image">
  <c:set var="imgSrc"><cms:contentshow /></c:set>
    <%
        String imgSrc = pageContext.getAttribute("imgSrc").toString();
        imageArr.add(imgSrc);
    %>
 </cms:contentloop>
<%
CmsJspActionElement cms = new CmsJspActionElement( pageContext, request, response );
		CmsObject cmso = cms.getCmsObject();
		
//获得第一张图片的字节数组
		CmsFile file = ( CmsFile )cmso.readFile( imageArr.get(0).toString() );
		byte[] buf2 = file.getContents();
%>
 关键是: CmsFile file = ( CmsFile )cmso.readFile( imageArr.get(0).toString() ); 这一句。

 

你可能感兴趣的:(C++,c,cms,C#,WAP)