springboto集成ckfinder的坑

https://my.oschina.net/zwolfking/blog/824753

springboog集成ckfinder中出现WEB-INF/ckfinder.xml 文件找不到,初始化失败 在开发工具里面可以以,打包以后有问题。主要是Configuration类中的 init 调用 getFullConfigPath方法导致的 springboot对 相对目录API不能使用,servletContext.getRealPath(“/”) 返回的NULL

(1) 不能把ckfinder.xml 放在WEB-INF 目录下面,放在classpath下面

(2) 自定义Configuration,改写init方法,主要是修改获取xml的方法,超类中用的是

File file = new File(ServletContextFactory.getServletContext().getRealPath(this.xmlFilePath));

将获取xml配置的方法改为 用ResourceLoader 从classpath 抓取

public void init() throws Exception {
    DefaultResourceLoader loader = new DefaultResourceLoader();
    Resource resource = loader.getResource(this.xmlFilePath);
    Class<?> clazz = getClass().getSuperclass();
    Field field = clazz.getDeclaredField("lastCfgModificationDate");
    Method method = clazz.getDeclaredMethod("clearConfiguration");
    method.setAccessible(true);
    method.invoke(this);
    field.setAccessible(true);
    field.set(this, System.currentTimeMillis());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(resource.getInputStream());
    doc.normalize();
    Node node = doc.getFirstChild();
    if (node != null) {
        NodeList nodeList = node.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node childNode = nodeList.item(i);
            if (childNode.getNodeName().equals("enabled"))
                this.enabled = Boolean.valueOf(childNode.getTextContent().trim()).booleanValue();

            if (childNode.getNodeName().equals("baseDir")) {
                this.baseDir = childNode.getTextContent().trim();
                this.baseDir = PathUtils.escape(this.baseDir);
                this.baseDir = PathUtils.addSlashToEnd(this.baseDir);
            }
            if (childNode.getNodeName().equals("baseURL")) {
                this.baseURL = childNode.getTextContent().trim();
                this.baseURL = PathUtils.escape(this.baseURL);
                this.baseURL = PathUtils.addSlashToEnd(this.baseURL);
            }
            if (childNode.getNodeName().equals("licenseName"))
                this.licenseName = childNode.getTextContent().trim();
            if (childNode.getNodeName().equals("licenseKey"))
                this.licenseKey = childNode.getTextContent().trim();
            String value;
            if (childNode.getNodeName().equals("imgWidth")) {
                value = childNode.getTextContent().trim();
                value = value.replaceAll("//D", "");

                try {
                    this.imgWidth = Integer.valueOf(value);
                } catch (NumberFormatException var13) {
                    this.imgWidth = null;
                }
            }
            if (childNode.getNodeName().equals("imgQuality")) {
                value = childNode.getTextContent().trim();
                value = value.replaceAll("//D", "");
                method = clazz.getDeclaredMethod("adjustQuality", new Class[]{String.class});
                method.setAccessible(true);
                this.imgQuality = Float.parseFloat(method.invoke(this, value).toString());
            }
            if (childNode.getNodeName().equals("imgHeight")) {
                value = childNode.getTextContent().trim();
                value = value.replaceAll("//D", "");
                try {
                    this.imgHeight = Integer.valueOf(value);
                } catch (NumberFormatException var12) {
                    this.imgHeight = null;
                }
            }
            if (childNode.getNodeName().equals("thumbs")) {
                method = clazz.getDeclaredMethod("setThumbs", new Class[]{NodeList.class});
                method.setAccessible(true);
                method.invoke(this, childNode.getChildNodes());
            }
            if (childNode.getNodeName().equals("accessControls")) {
                method = clazz.getDeclaredMethod("setACLs", new Class[]{NodeList.class});
                method.setAccessible(true);
                method.invoke(this, childNode.getChildNodes());
            }
            if (childNode.getNodeName().equals("hideFolders")) {
                method = clazz.getDeclaredMethod("setHiddenFolders", new Class[]{NodeList.class});
                method.setAccessible(true);
                method.invoke(this, childNode.getChildNodes());
            }
            if (childNode.getNodeName().equals("hideFiles")) {
                method = clazz.getDeclaredMethod("setHiddenFiles", new Class[]{NodeList.class});
                method.setAccessible(true);
                method.invoke(this, childNode.getChildNodes());
            }
            if (childNode.getNodeName().equals("checkDoubleExtension"))
                this.doubleExtensions = Boolean.valueOf(childNode.getTextContent().trim()).booleanValue();
            if (childNode.getNodeName().equals("disallowUnsafeCharacters"))
                this.disallowUnsafeCharacters = Boolean.valueOf(childNode.getTextContent().trim()).booleanValue();
            if (childNode.getNodeName().equals("forceASCII"))
                this.forceASCII = Boolean.valueOf(childNode.getTextContent().trim()).booleanValue();
            if (childNode.getNodeName().equals("checkSizeAfterScaling"))
                this.checkSizeAfterScaling = Boolean.valueOf(childNode.getTextContent().trim()).booleanValue();
            Scanner sc;
            if (childNode.getNodeName().equals("htmlExtensions")) {
                value = childNode.getTextContent();
                sc = (new Scanner(value)).useDelimiter(",");
                while (sc.hasNext()) {
                    String val = sc.next();
                    if (val != null && !val.equals(""))
                        this.htmlExtensions.add(val.trim().toLowerCase());
                }
            }
            if (childNode.getNodeName().equals("secureImageUploads"))
                this.secureImageUploads = Boolean.valueOf(childNode.getTextContent().trim()).booleanValue();
            if (childNode.getNodeName().equals("uriEncoding"))
                this.uriEncoding = childNode.getTextContent().trim();
            if (childNode.getNodeName().equals("userRoleSessionVar"))
                this.userRoleSessionVar = childNode.getTextContent().trim();
            if (childNode.getNodeName().equals("defaultResourceTypes")) {
                value = childNode.getTextContent().trim();
                sc = (new Scanner(value)).useDelimiter(",");
                while (sc.hasNext())
                    this.defaultResourceTypes.add(sc.next());
            }
            if (childNode.getNodeName().equals("plugins")) {
                method = clazz.getDeclaredMethod("setPlugins", new Class[]{Node.class});
                method.setAccessible(true);
                method.invoke(this, childNode);

            }
            if (childNode.getNodeName().equals("basePathBuilderImpl")) {
                method = clazz.getDeclaredMethod("setBasePathImpl", new Class[]{String.class});
                method.setAccessible(true);
                method.invoke(this, childNode.getTextContent().trim());
            }
        }
    }
    method = clazz.getDeclaredMethod("setTypes", new Class[]{Document.class});
    method.setAccessible(true);
    method.invoke(this, doc);
    field = clazz.getDeclaredField("events");
    field.setAccessible(true);
    field.set(this, new Events());
    this.registerEventHandlers();
}

(3)自定义ckfinder的servlet

@WebServlet(urlPatterns = "/static/ckfinder/core/connector/java/connector.java", initParams = {
        @WebInitParam(name = "XMLConfig", value = "classpath:ckfinder.xml"),
        @WebInitParam(name = "debug", value = "false"),
        @WebInitParam(name = "configuration", value = "com.wolfking.jeesite.common.web.CKFinderConfig")
}, loadOnStartup = 1)
public class CKFinderConnectorServlet extends ConnectorServlet {

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response) throws ServletException, IOException {
        prepareGetResponse(request, response, false);
        super.doGet(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        prepareGetResponse(request, response, true);
        super.doPost(request, response);
    }

    private void prepareGetResponse(final HttpServletRequest request,
                                    final HttpServletResponse response, final boolean post) throws ServletException {
        Principal principal = (Principal) UserUtils.getPrincipal();
        if (principal == null) {
            return;
        }
        String command = request.getParameter("command");
        String type = request.getParameter("type");
        // 初始化时,如果startupPath文件夹不存在,则自动创建startupPath文件夹
        if ("Init".equals(command)) {
            String startupPath = request.getParameter("startupPath");// 当前文件夹可指定为模块名
            if (startupPath != null) {
                String[] ss = startupPath.split(":");
                if (ss.length == 2) {
                    String realPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL
                            + principal + "/" + ss[0] + ss[1];
                    FileUtils.createDirectory(FileUtils.path(realPath));
                }
            }
        }
        // 快捷上传,自动创建当前文件夹,并上传到该路径
        else if ("QuickUpload".equals(command) && type != null) {
            String currentFolder = request.getParameter("currentFolder");// 当前文件夹可指定为模块名
            String realPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL
                    + principal + "/" + type + (currentFolder != null ? currentFolder : "");
            FileUtils.createDirectory(FileUtils.path(realPath));
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值