整合CKEditor和CKFinder(for java)

https://cksource.com/ckfinder/download

 

下载地址 CKEditor 
我下载的版本ckfinder_java_2.4 
解压之后,会看到一个CKFinderJava-2.4.war文件,直接放到Tomcat/webapps发布目录下,把服务打开就自动解压了,然后将ckfinder目录Copy到你项目WebRoot下。 
步骤一:将CKFinderJava-2.4下的jar包Copy到你的项目下,还有WEB-INF/config.xml文件Copy到你的项目WEB-INF/下。 
步骤二:修改config.xml文件(说明:CKFinderJava/userfiles/目录存储文件的目录) 

<enabled>true</enabled>
<baseURL>/CKEditorAndCKFinder/CKFinderJava/userfiles/</baseURL>

步骤三:在web.xml文件,配置Servlet 

<servlet>
<servlet-name>ConnectorServlet</servlet-name>
<servlet-class>com.ckfinder.connector.ConnectorServlet</servlet-class>
<init-param>
<param-name>XMLConfig</param-name>
<param-value>/WEB-INF/config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ConnectorServlet</servlet-name>
<url-pattern>
/ckfinder/core/connector/java/connector.java
</url-pattern>
</servlet-mapping>
<filter>
<filter-name>FileUploadFilter</filter-name>
<filter-class>com.ckfinder.connector.FileUploadFilter</filter-class>
<init-param>
<param-name>sessionCookieName</param-name>
<param-value>JSESSIONID</param-value>
</init-param>
<init-param>
<param-name>sessionParameterName</param-name>
<param-value>jsessionid</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FileUploadFilter</filter-name>
<url-pattern>
/ckfinder/core/connector/java/connector.java
     	 </url-pattern>
</filter-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>

步骤四:修改ckeditor/config.js文件(当前也可以是自己编写的js配置文件,上一篇介绍过) 

config.filebrowserBrowseUrl =  '/CKEditorAndCKFinder/ckfinder/ckfinder.html' ;  
config.filebrowserImageBrowseUrl =  '/CKEditorAndCKFinder/ckfinder/ckfinder.html?type=Images' ;  
config.filebrowserFlashBrowseUrl =  '/CKEditorAndCKFinder/ckfinder/ckfinder.html?type=Flash' ;  
config.filebrowserUploadUrl =  '/CKEditorAndCKFinder/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Files' ;  
config.filebrowserImageUploadUrl =  '/CKEditorAndCKFinder/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Images' ;  
config.filebrowserFlashUploadUrl =  '/CKEditorAndCKFinder/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Flash' ;  
config.filebrowserWindowWidth = '1000';  
config.filebrowserWindowHeight = '700';  
config.language =  "zh-cn" ;

关于文件名乱码,图片不能显示问题。 
解决方案一:修改Tomcat web.xml配置文件 
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="6060" protocol="HTTP/1.1" redirectPort="8443"/> 
当然,该方式不合适,导致全部站点使用同样的编码方式(对于GET请求还存在其它问题)。 
解决方案二: 
修改源代码,实现文件重命名 
1、通过UUID.randomUUID().toString() 
2、日期命名法 
我创建新的项目,将CKFinder源码导入,修改FileUploadCommand类的saveTemporaryFile方法,将newFileName重命令即可。 
1、用压缩软件打开jar包,找到修改文件的路径。 
2、把编译好的class文件添加到jar包的该路径下替换原来的文件 
3、收工! 

 

		this.newFileName = "bjs-"+UUID.randomUUID().toString()+"."+FileUtils.getFileExtension(this.newFileName);
		File file = new File(path, this.newFileName);

 

     1. 从http://ckeditor.com中下载ckeditor for java和ckfinder for java。

        2. 将下载的文件解压,将ckeditor文件夹和ckfinder文件夹放置到项目根目录下,其中ckfinder文件夹在ckfinder*.war中。

        3. ckfinder*.war的WEB-INF目录下有一个config.xml文件,将之放置在项目的WEB-INF目录下,打开它,并进行相应修改:

 

[java]  view plain copy
 
  1. <config>  
  2.     <enabled><span>true</span></enabled><!--是否开启图片上传功能--!>  
  3.     <baseDir></baseDir>  
  4.     <baseURL><span>/ruisystem/userfiles/</span></baseURL><!--图片上传后的位置-->  
  5.     <licenseKey></licenseKey>  
  6.     <licenseName></licenseName>  
  7.     <imgWidth>1600</imgWidth>  
  8.     <imgHeight>1200</imgHeight>  
  9.     <imgQuality>80</imgQuality>  
  10.     <uriEncoding><span>UTF-8</span></uriEncoding><!--编码-->  
  11.     <forceASCII>false</forceASCII>  
  12.         <disallowUnsafeCharacters>false</disallowUnsafeCharacters>  
  13.     <userRoleSessionVar>CKFinder_UserRole</userRoleSessionVar>  
  14.     <checkDoubleExtension>true</checkDoubleExtension>  
  15.     <checkSizeAfterScaling>true</checkSizeAfterScaling>  
  16.     <secureImageUploads>true</secureImageUploads>  
  17.     <htmlExtensions>html,htm,xml,js</htmlExtensions>  


        4. 打开ckeditor/config.js作相应修改,以下是我的修改:

[java]  view plain copy
 
  1. /* 
  2. Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. 
  3. For licensing, see LICENSE.html or http://ckeditor.com/license 
  4.  */  
  5.   
  6. CKEDITOR.editorConfig = function(config) {  
  7.     // Define changes to default configuration here. For example:  
  8.     // config.language = 'fr';  
  9.     // config.uiColor = '#AADC6E';  
  10.     config.height = '700px';  
  11.     config.font_names = '新细明体/PMingLiU;细明体/MingLiU;标楷体/DFKai-SB;黑体/SimHei;宋体/SimSun;新宋体/NSimSun;仿宋/FangSong;楷体/KaiTi;仿宋_GB2312/FangSong_GB2312楷体_GB2312/KaiTi_GB2312;微軟正黑體/Microsoft JhengHei;微软雅黑体/Microsoft YaHei;隶书/LiSu;幼圆/YouYuan;华文细黑/STXihei;华文楷体/STKaiti;华文宋体/STSong;华文中宋/STZhongsong;华文仿宋/STFangsong;方正舒体/FZShuTi;方正姚体/FZYaoti;华文彩云/STCaiyun;华文琥珀/STHupo;华文隶书/STLiti;华文行楷/STXingkai;华文新魏/STXinwei;'  
  12.             + config.font_names;  
  13. };  

        5. 将ckeditor-java-core-v.jar和ckfinder*.war的WEB-INF/lib目录下的所有jar包复制到项目中。

 

        6. 打开要使用ckeditor的页面,首先引入两个js:

 

[java]  view plain copy
 
  1. <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>  
  2. <script type="text/javascript" src="/ckfinder/ckfinder.js"></script>  


        7. 在需要使用ckeditor的域下面添加一些代码:

 

 

[java]  view plain copy
 
  1. <textarea rows="2" cols="2" name="content" id="content"></textarea>   
  2. <script type="text/javascript">  
  3. if (CKEDITOR.instances['content']) {  
  4.     CKEDITOR.remove(CKEDITOR.instances['content']);  
  5. }  
  6.   
  7. var basePath = '你的位置';  
  8.   
  9. CKEDITOR  
  10.         .replace(  
  11.                 'content',  
  12.                 {  
  13.                     filebrowserBrowseUrl : basePath  
  14.                             + '/ckfinder/ckfinder.htm',  
  15.                     filebrowserImageBrowseUrl : basePath  
  16.                             + '/ckfinder/ckfinder.htm?type=Images',  
  17.                     filebrowserFlashBrowseUrl : basePath  
  18.                             + '/ckfinder/ckfinder.htm?type=Flash',  
  19.                     filebrowserUploadUrl : basePath  
  20.                             + '/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Files',  
  21.                     filebrowserImageUploadUrl : basePath  
  22.                             + '/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Images',  
  23.                     filebrowserFlashUploadUrl : basePath  
  24.                             + '/ckfinder/core/connector/java/connector.java?command=QuickUpload&type=Flash',  
  25.                     filebrowserWindowWidth : '1000',  
  26.                     filebrowserWindowHeight : '1000'  
  27.                 });  
  28. </script>  


        8. 在web.xml中添加如下代码:

 

 

[java]  view plain copy
 
  1. <filter>  
  2.     <filter-name>FileUploadFilter</filter-name>  
  3.     <filter-class>com.ckfinder.connector.FileUploadFilter</filter-class>  
  4.     <init-param>  
  5.         <param-name>sessionCookieName</param-name>  
  6.         <param-value>JSESSIONID</param-value>  
  7.     </init-param>  
  8.     <init-param>  
  9.         <param-name>sessionParameterName</param-name>  
  10.         <param-value>jsessionid</param-value>  
  11.     </init-param>  
  12. </filter>  
  13. <filter-mapping>  
  14.     <filter-name>FileUploadFilter</filter-name>  
  15.     <url-pattern>/ckfinder/core/connector/java/connector.java</url-pattern>  
  16. </filter-mapping>  
  17. <servlet>  
  18.     <servlet-name>ConnectorServlet</servlet-name>  
  19.     <servlet-class>com.ckfinder.connector.ConnectorServlet</servlet-class>  
  20.     <init-param>  
  21.         <param-name>XMLConfig</param-name>  
  22.         <param-value>/WEB-INF/config.xml </param-value>  
  23.     </init-param>  
  24.     <init-param>  
  25.         <param-name>debug</param-name>  
  26.         <param-value> false </param-value>  
  27.     </init-param>  
  28.     <load-on-startup> 1 </load-on-startup>  
  29. </servlet>  
  30. <servlet-mapping>  
  31.     <servlet-name>ConnectorServlet</servlet-name>  
  32.     <url-pattern>/ckfinder/core/connector/java/connector.java</url-pattern>  
  33. </servlet-mapping>  


        启动tomcat,开始体验吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值