很早以前就想把CKEditor在JSP下的图片上传及浏览服务器图片的方法写下来了,不过因为教学项目中要用到,担心HEM直接套用,自己不去调查(我可是用心良苦啊),不能很好的锻炼,一直没写出来,今天项目开始测试了,他们的功能也都基本可以结束了,我也可以发我的帖了。
写这个的起因是在网上一仁兄的帖子,抱怨说CKEditor不支持JSP了,感叹了许多,说支持ASP、PHP,就是不支持JSP,于是俺也在网上找了找JSP方面的资料,看来确实不支持了,不过人家也是有道理的,毕竟JSP上传的图片,不能简单的用个JSP就随便搞搞就O了。
关于CKEditor在JSP下上传图片的方法,网上有很多,大都是写了一大堆JS代码然后自己注册一个事件,写的太多,我没怎么看懂,不过有一位大侠写的让我看到了简单写法的曙光(不过遗憾的是,时间太长了,不知道大侠的URL了)。
言归正传,对于上传CKEditor已经做好了,我们只要准备个功能,接收CKEditor提交过来的文件就可以了,所以呢实现的思路是:
说明一下,之所以采用JSP没有使用Servlet,那是因为JSP简单啊,这样可以降低CKEditor对项目的侵入性啊。下面看代码啦:
OK,修改完毕。简单吧,其实上传和浏览文件很简单(上面只是一个演示,代码仅供参考),要点是要调用一下CKEDITOR.tools.callFunction方法。
附件打包了一个可运行的Eclipse工程,供需要的朋友参考。