关于opencms把sites目录中的文件根据URL显示到网页的解决方法

   由于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 >
<%
    } 
%>   

 

欢迎提出新的意见!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值