由于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
>
第二、新增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
>
<%
}
%>
欢迎提出新的意见!