?? 由于web中的文件不能直接通過url方式讀取opencms的sites下的文件,所以必須繞個彎子解決。查閱了一下opencms的源代碼,我的解決思路如下:
?
第一 拷貝方式:
?
?? 1.第一次訪問時,根據opencms提供的接口,把文件從數據庫讀到java對象obj
?? 2.將obj對象拷貝到web中的對應目錄
?? 3.將web中的對應目錄的URL確定,即可直接調用。
?? 4.第二次及其以后訪問時,根據對象obj的最后修改時間屬性,確定是否需要重復1-3步驟。
參考源代碼:
?
參考源代碼:
?
<!--
?$Id:?test.jsp,v?
1.5
?
2006
/
08
/
11
?
04
:
59
:
31
?davidye?Exp?$
-->
? <% @?page?pageEncoding = " UTF-8 " ? %>
<% @?page?buffer = " none " ? import = " org.opencms.util.*,?org.teema.eeonline.exhibition.ExhibitionConstants,com.tblink.common.util.StringUtil,org.opencms.frontend.templateone.modules.*,javax.servlet.ServletOutputStream,?org.opencms.file.*,java.util.*,?java.io.*,?org.opencms.frontend.templateone.* " ? %>
<% @?taglib?uri = " http://jakarta.apache.org/struts/tags-bean " ?prefix = " bean " %>
<% @?taglib?prefix = " cms " ?uri = " http://www.opencms.org/taglib/cms " ? %>
<% @?taglib?prefix = " fmt " ?uri = " http://java.sun.com/jstl/fmt " ? %>
<% @?taglib?prefix = " c " ?uri = " http://java.sun.com/jstl/core " ? %>
<% @?include?file = " ../elements/common/common.jsp " ? %>
<%!
private ?String?copyFile(CmsFile?cmsFile){
???? try {
???????? byte []?byteFile? = ?cmsFile.getContents();
????????String?path? = ?StringUtil.getRealPath(StringUtil. class , " WEB-INF " ,ExhibitionConstants.EXHIBITION_FLASH_DIRECTIONARY);
????????RandomAccessFile?rf = new ?RandomAccessFile(path? + ?ExhibitionConstants.EXHIBITION_FLASH_NAME, " rw " );?
????????rf.write(byteFile);?
????????rf.close();
???????? return ?path.substring( 0 ,path.length() - 1 );
????}
???? catch (Exception?e)
????{
???????? return ? "" ;
????}
}
%>
<%
String?direct? = ?ExhibitionConstants.EXHIBITION_FLASH_DIRECTIONARY? + ?ExhibitionConstants.EXHIBITION_FLASH_NAME;
String?flashUrl? = ? "" ;
try {
CmsFile?cmsFile? = ?__cmsJSP.getCmsObject().readFile(direct);
long ?l? = ?cmsFile.getDateLastModified();
String?lstr? = ?(String)application.getAttribute(ExhibitionConstants.APPLICATION_FLASH_DATE_INDEX);
flashUrl? = ?(String)application.getAttribute(ExhibitionConstants.APPLICATION_FLASH_URL_INDEX);
if (lstr == null ? || ? ! StringUtil.isNums(lstr)){
????direct? = ?copyFile(cmsFile);
} else ? if (Long.parseLong(lstr) == l){
???? if (flashUrl == null ){
????????direct? = ?copyFile(cmsFile);
????}
} else {
??????direct? = ?copyFile(cmsFile);
}
flashUrl? = ??request.getContextPath()? + ?ExhibitionConstants.EXHIBITION_FLASH_DIRECTIONARY? + ?ExhibitionConstants.EXHIBITION_FLASH_NAME;
application.setAttribute(ExhibitionConstants.APPLICATION_FLASH_DATE_INDEX,lstr);
application.setAttribute(ExhibitionConstants.APPLICATION_FLASH_URL_INDEX,flashUrl);
}
catch (Exception?e)
{
????out.println(e);?
}
%>
< object?classid = " clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 " ?codebase = " http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 " ?width = " 760 " ?height = " 266 " >
??????? < param?name = " movie " ?value = " <%=flashUrl%> " >
??????? < param?name = " quality " ?value = " high " >
??????? < embed?src = " <%=flashUrl%> " ?quality = " high " ?pluginspage = " http://www.macromedia.com/go/getflashplayer " ?type = " application/x-shockwave-flash " ?width = " 760 " ?height = " 266 " ></ embed >
</ object >
? <% @?page?pageEncoding = " UTF-8 " ? %>
<% @?page?buffer = " none " ? import = " org.opencms.util.*,?org.teema.eeonline.exhibition.ExhibitionConstants,com.tblink.common.util.StringUtil,org.opencms.frontend.templateone.modules.*,javax.servlet.ServletOutputStream,?org.opencms.file.*,java.util.*,?java.io.*,?org.opencms.frontend.templateone.* " ? %>
<% @?taglib?uri = " http://jakarta.apache.org/struts/tags-bean " ?prefix = " bean " %>
<% @?taglib?prefix = " cms " ?uri = " http://www.opencms.org/taglib/cms " ? %>
<% @?taglib?prefix = " fmt " ?uri = " http://java.sun.com/jstl/fmt " ? %>
<% @?taglib?prefix = " c " ?uri = " http://java.sun.com/jstl/core " ? %>
<% @?include?file = " ../elements/common/common.jsp " ? %>
<%!
private ?String?copyFile(CmsFile?cmsFile){
???? try {
???????? byte []?byteFile? = ?cmsFile.getContents();
????????String?path? = ?StringUtil.getRealPath(StringUtil. class , " WEB-INF " ,ExhibitionConstants.EXHIBITION_FLASH_DIRECTIONARY);
????????RandomAccessFile?rf = new ?RandomAccessFile(path? + ?ExhibitionConstants.EXHIBITION_FLASH_NAME, " rw " );?
????????rf.write(byteFile);?
????????rf.close();
???????? return ?path.substring( 0 ,path.length() - 1 );
????}
???? catch (Exception?e)
????{
???????? return ? "" ;
????}
}
%>
<%
String?direct? = ?ExhibitionConstants.EXHIBITION_FLASH_DIRECTIONARY? + ?ExhibitionConstants.EXHIBITION_FLASH_NAME;
String?flashUrl? = ? "" ;
try {
CmsFile?cmsFile? = ?__cmsJSP.getCmsObject().readFile(direct);
long ?l? = ?cmsFile.getDateLastModified();
String?lstr? = ?(String)application.getAttribute(ExhibitionConstants.APPLICATION_FLASH_DATE_INDEX);
flashUrl? = ?(String)application.getAttribute(ExhibitionConstants.APPLICATION_FLASH_URL_INDEX);
if (lstr == null ? || ? ! StringUtil.isNums(lstr)){
????direct? = ?copyFile(cmsFile);
} else ? if (Long.parseLong(lstr) == l){
???? if (flashUrl == null ){
????????direct? = ?copyFile(cmsFile);
????}
} else {
??????direct? = ?copyFile(cmsFile);
}
flashUrl? = ??request.getContextPath()? + ?ExhibitionConstants.EXHIBITION_FLASH_DIRECTIONARY? + ?ExhibitionConstants.EXHIBITION_FLASH_NAME;
application.setAttribute(ExhibitionConstants.APPLICATION_FLASH_DATE_INDEX,lstr);
application.setAttribute(ExhibitionConstants.APPLICATION_FLASH_URL_INDEX,flashUrl);
}
catch (Exception?e)
{
????out.println(e);?
}
%>
< object?classid = " clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 " ?codebase = " http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0 " ?width = " 760 " ?height = " 266 " >
??????? < param?name = " movie " ?value = " <%=flashUrl%> " >
??????? < param?name = " quality " ?value = " high " >
??????? < embed?src = " <%=flashUrl%> " ?quality = " high " ?pluginspage = " http://www.macromedia.com/go/getflashplayer " ?type = " application/x-shockwave-flash " ?width = " 760 " ?height = " 266 " ></ embed >
</ object >
第二、新增Resource Type方式
? 1.新建Resource Type,詳細請見前一篇文章
??2.在opencms控制臺,新增該類別的文件
? 3.調用相關API顯示出來,代碼如下
?
<%
????if?(getResourceCount(__cms,?"TEEMA_Ad/Home/Wide_Block_1/|teemaAd",?false)?>?0)?{?
%>??
????????????<cms:contentload?collector="allInFolderDateReleasedDesc"?param="TEEMA_Ad/Home/Wide_Block_1/|teemaAd"?editable="true">
????????????????????<%?
?????????????????if(bannerTextCount<1)?{
????????????????bannerTextCount++;
????????????%>
????????????????????????????<a?href='<cms:contentshow?element="URL"/>'?target="_blank">
??????????????????????????????<img?src="/eeonline/cms<cms:contentshow?element="Image/Image"/>"?width="440"?height="75">
????????????????????????????</a>
????????????<%???}?%>
??????????????????</cms:contentload>
????????</tr>
<%
????}?
%>??
????if?(getResourceCount(__cms,?"TEEMA_Ad/Home/Wide_Block_1/|teemaAd",?false)?>?0)?{?
%>??
????????????<cms:contentload?collector="allInFolderDateReleasedDesc"?param="TEEMA_Ad/Home/Wide_Block_1/|teemaAd"?editable="true">
????????????????????<%?
?????????????????if(bannerTextCount<1)?{
????????????????bannerTextCount++;
????????????%>
????????????????????????????<a?href='<cms:contentshow?element="URL"/>'?target="_blank">
??????????????????????????????<img?src="/eeonline/cms<cms:contentshow?element="Image/Image"/>"?width="440"?height="75">
????????????????????????????</a>
????????????<%???}?%>
??????????????????</cms:contentload>
????????</tr>
<%
????}?
%>??
?
歡迎提出新的意見!