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>
结束运行看效果。