JFinal整合ckeditor+CKFinder
博客专区 > jsspring 的博客 > 博客详情
JFinal整合ckeditor+CKFinder
jsspring 发表于2年前
JFinal整合ckeditor+CKFinder
  • 发表于 2年前
  • 阅读 60
  • 收藏 3
  • 点赞 1
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: JFinal、ckeditor、CKFinder,需要不同用户只能查看自己已经上传的图片("upload/用户ID/Images/")。

1.效果图如下:


src目录下新增ckfinder.properties:

debug=false
XMLConfig=/WEB-INF/ckfinder.xml
添加CKFinderHandler:
package com.jfaker.app.ext;


import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ckfinder.connector.ConnectorServlet;
import com.jfinal.handler.Handler;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
 
public class CKFinderHandler extends Handler
{
    private String visitPath = "/styles/ckfinder/core/connector/java/connector";
     
    private ConnectorServlet servlet = new ConnectorServlet();
     
    private Properties properties;
     
    public CKFinderHandler() {}
     
    public CKFinderHandler(String cfgPath) 
    {
    	Prop prop = PropKit.use(cfgPath, "UTF-8");
        this.properties = prop.getProperties();
    }
     
    public CKFinderHandler(String visitPath, String cfgPath)
    {
        this.visitPath = visitPath;
        Prop prop = PropKit.use(cfgPath, "UTF-8");
        this.properties = prop.getProperties();
    }
     
    @Override
    public void handle(String target, final HttpServletRequest request, HttpServletResponse response, boolean[] isHandled)
    {
        if (target.startsWith(visitPath)) {
            isHandled[0] = true;
             
            try {
                servlet.init(new ServletConfig()
                {
                    @Override
                    public String getServletName() {return null; }
                     
                    @Override
                    public ServletContext getServletContext() {return request.getSession().getServletContext(); }
                     
                    @SuppressWarnings("rawtypes")
                    @Override
                    public Enumeration getInitParameterNames() {return null; }
                     
                    @Override
                    public String getInitParameter(String name)
                    {
                        return properties.getProperty(name);
                    }
                });
                servlet.service(request, response);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        else {
            nextHandler.handle(target, request, response, isHandled);
        }
    }
}
AppConfig中配置handler:
me.add(new CKFinderHandler("ckfinder.properties"));
由于不同用户baseUrl不同,所以需要克制FckeditorUserPathBuilder
package com.jfaker.app.ext;

import javax.servlet.http.HttpServletRequest;

import com.ckfinder.connector.configuration.ConfigurationFactory;
import com.ckfinder.connector.configuration.DefaultPathBuilder;
import com.ckfinder.connector.configuration.IConfiguration;
import com.ckfinder.connector.utils.PathUtils;
import com.jfaker.app.shiro.ShiroUtils;

public class FckeditorUserPathBuilder extends DefaultPathBuilder {
	public String getBaseUrl(HttpServletRequest request) {
		String baseURL;
		try {
			IConfiguration conf = ConfigurationFactory.getInstace().getConfiguration();
			baseURL = conf.getBaseURL();
		} catch (Exception e) {
			baseURL = null;
		}
		if ((baseURL == null) || (baseURL.equals(""))) {
			baseURL = super.getBaseUrl(request);
		}
		baseURL= baseURL+"user/"+ShiroUtils.getUserId()+"/";
		return PathUtils.addSlashToBeginning(PathUtils.addSlashToEnd(baseURL));
	}

	public String getBaseDir(HttpServletRequest request) {
		String baseDir;
		try {
			IConfiguration conf = ConfigurationFactory.getInstace().getConfiguration();
			baseDir = conf.getBaseDir();
		} catch (Exception e) {
			baseDir = null;
		}
		if ((baseDir == null) || (baseDir.equals(""))) {
			return super.getBaseDir(request);
		}
		return baseDir;
	}
}
ckfinder.xml中修改basePathBuilderImpl:
<basePathBuilderImpl>com.jfaker.app.ext.FckeditorUserPathBuilder</basePathBuilderImpl>
结束运行看效果。


共有 人打赏支持
粉丝 2
博文 11
码字总数 4379
×
jsspring
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: