欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Kindeditor

程序员文章站 2022-04-13 13:12:40
...

Kindeditor上传图片和查询图片库

<html>
<head>
    <script charset="utf-8" src="kindeditor-all-min.js"></script>
    <script charset="utf-8" src="lang/zh-CN.js"></script>
    <script>
        KindEditor.ready(function(K) {
            window.editor = K.create('#editor_id',{
                uploadJson:"${pageContext.request.contextPath}/kindeditor/upload",
                filePostName:"img",
                fileManagerJson:"${pageContext.request.contextPath}/kindeditor/getAllImg",
                allowFileManager:true
            });
        });
    </script>
</head>
<body>
    <textarea id="editor_id" name="content" style="width:700px;height:300px;">
        &lt;strong&gt;HTML内容&lt;/strong&gt;
    </textarea>
</body>
</html>

后台代码

返回的数据格式有点复杂,需要自己封装

@RequestMapping("/upload")
    public Map<String,Object> upload(HttpServletRequest request, MultipartFile img) throws IOException {
        Map<String, Object> map = new HashMap<>();
        String realPath = request.getSession().getServletContext().getRealPath("/upload/");
        File file = new File(realPath);
        if (!file.exists()){
            file.mkdir();
        }
        String filename = img.getOriginalFilename();
        String newFileName =new Date().getTime() + "-" + filename;
        img.transferTo(new File(realPath,newFileName));
        map.put("error",0);
        //动态获取url
        String scheme = request.getScheme();
        InetAddress localHost = Inet4Address.getLocalHost();
        String hostAddress = localHost.getHostAddress();
        int port = request.getServerPort();
        String contextPath = request.getContextPath();
        String url = scheme + "://" + hostAddress + ":" + port + contextPath + "/upload/" + newFileName;
        map.put("url",url);
        return map;
    }

    @RequestMapping("getAllImg")
    public Map<String,Object> getAllImg(HttpServletRequest request) throws UnknownHostException {
        Map<String, Object> map = new HashMap<>();
        List<Map<String ,Object>> list = new ArrayList<>();

        String realPath = request.getSession().getServletContext().getRealPath("/upload/");
        File file = new File(realPath);
        String[] names = file.list();
        for (String name : names) {
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("is_dir",false);
            map1.put("has_file",false);
            File file1 = new File(realPath, name);
            long length = file1.length();
            map1.put("filesize",length);
            map1.put("is_photo",true);
            String s = name.substring(name.lastIndexOf(".") + 1);
            map1.put("filetype",s);
            map1.put("filename",name);
            boolean b = name.contains("-");
            if (b){
                String s1 = name.split("-")[0];
                Long aLong = Long.valueOf(s1);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String s2 = format.format(aLong);
                map1.put("datetime",s2);
            }
            if (!b){
                map1.put("datetime",new Date());
            }
            map1.put("dir_path","");
            list.add(map1);
        }
        String scheme = request.getScheme();
        InetAddress localHost = Inet4Address.getLocalHost();
        String address = localHost.getHostAddress();
        int port = request.getServerPort();
        String path = request.getContextPath();
        String url=scheme+"://"+address+":"+port+path+"/upload/";

        map.put("moveup_dir_path","");
        map.put("current_dir_path","");
        map.put("current_url",url);
        map.put("total_count",names.length);
        map.put("file_list",list);

        return map;
    }
相关标签: Kindeditor