SpringBoot集成ckfinder3.5.1

☆樱花仙子☆ 提交于 2020-01-14 09:39:18

1、下载ckfinder3.5.1

地址:https://ckeditor.com/ckfinder/download/#java
ckfinder3.5.1下载

2、创建springboot项目并导入相关的ckfinder文件

创建springboot项目的过程此处不再进行展示,创建完项目后就把刚才下载的ckfinder中的相关文件拷贝到项目中。如下图把标记的两个文件直接复制拷贝到项目的java路径下。
文件路径
拷贝项目后的展示

3、导入ckfinder的maven依赖

在复制拷贝上面文件后会有一些报错是关于没有相关的ckfinder的jar,然后我们需要在pom.xml中导入相关依赖,如下图为我的pom.xml的内容,标记的地方为ckfinder依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- ckfinder依赖 -->
        <dependency>
            <groupId>com.cksource</groupId>
            <artifactId>ckfinder3</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

   <!-- ckfinder依赖 -->
    <repositories>
        <repository>
            <id>cksource-mvn-repo</id>
            <name>CKSource Maven Repository</name>
            <url>https://maven.cksource.com/</url>
        </repository>
    </repositories>
</project>

4、修改相关配置

1、复制拷贝ckfinder.yml到resources文件夹下然后把第一行的enabled:false改为true。

2、修改启动类注册ckfinder的servlet,下面为启动类代码。

package com.example.demo;

import com.cksource.ckfinder.servlet.CKFinderServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;

@SpringBootApplication
public class DemoApplication implements ServletContextInitializer, WebMvcConfigurer {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void onStartup(ServletContext servletContext) {
        // Register the CKFinder's servlet.
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("ckfinder", new CKFinderServlet());
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/ckfinder/*");
        //这个地方一定要注意为你的项目的下的ckinder文件路径
        dispatcher.setInitParameter("scan-path", "com.example.demo.ckfinder");//一定要修改为自己的项目路径

        FilterRegistration.Dynamic filter = servletContext.addFilter("x-content-options", new Filter() {
            @Override
            public void init(FilterConfig filterConfig) {
            }

            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                ((HttpServletResponse) response).setHeader("X-Content-Type-Options", "nosniff");
                chain.doFilter(request, response);
            }

            @Override
            public void destroy() {
            }
        });

        filter.addMappingForUrlPatterns(null, false, "/userfiles/*");

        String tempDirectory;

        try {
            tempDirectory = Files.createTempDirectory("ckfinder").toString();
        } catch (IOException e) {
            tempDirectory = null;
        }

        dispatcher.setMultipartConfig(new MultipartConfigElement(tempDirectory));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Configure the resource handler to serve files uploaded with CKFinder.
        String publicFilesDir = String.format("file:%s/userfiles/", System.getProperty("user.dir"));

        registry.addResourceHandler("/userfiles/**")
                .addResourceLocations(publicFilesDir);
    }

}

3、修改CustomConfigLoader类中的访问你的ckfinder.yml的路径

package com.example.demo.ckfinder.config;

import com.cksource.ckfinder.config.Config;
import com.cksource.ckfinder.config.loader.ConfigLoader;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;

import javax.inject.Named;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Named
public class CustomConfigLoader implements ConfigLoader {
    @Override
    public Config loadConfig() throws Exception {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        //这里你自己项目的ckinder.yml的路径,我的放在resources下面
        Path configPath = Paths.get(System.getProperty("user.dir")+"\\src\\main\\resources", "ckfinder.yml");
        return mapper.readValue(Files.newInputStream(configPath), CustomConfig.class);
    }
}

4、修改IndexController中需要访问的ckfinder.html页面代码

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
public class IndexController {
    @RequestMapping("/ckfinder.html")
    public void index(HttpServletResponse response) {
        // Redirect to CKFinder's samples.
        response.setHeader("Location", "/ckfinder/static/ckfinder.html");
        response.setStatus(302);
    }
}

5、启动访问并配置文件上传路径

1、配置成功后访问http://localhost:8081/ckfinder.html

 @RequestMapping("/ckfinder.html")配置的访问路径

访问成功

2、配置上传路径

这里我没有做修改按照文件原本的配置,如果修改看我标记的地方
配置1
配置2
然后在项目根路径下创建userfiles文件
上传文件路径

3、测试上传

在这里插入图片描述
在这里插入图片描述
到此简单的springboot集成ckfinder3.5.1已经全部做完!大家共同学习!!!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!