解决java Spring Mvc富文本编辑器CKEditor4图片上传问题

1.CKEditor4.22 config.js配置:

javascript 复制代码
 /*开启工具栏"图像"中文件上传功能,后面的url为图片上传要指向的的action或servlet*/
    config.filebrowserImageUploadUrl= "Manage/upload.htmls";
/*去掉图片预览框的文字*/
    config.image_previewText = ' ';
    //隐藏"超链接"与"高级选项"只留上传和预览按钮
    config.removeDialogTabs = 'image:advanced;image:Link;','help';
//解决[CKEDITOR] Error code: exportpdf-no-token-url.
//[CKEDITOR] For more information about this error go to 错误问题
 config.removePlugins = 'easyimage,cloudservices';
其它自己配置了

2.上传图片工具类UpLoadUtils(还可以完善)

java 复制代码
package com.common;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.io.File;
import java.util.Date;

/**
 * @标题:
 * @作者:eyucom-QMD
 * @描述:
 * @时间:2024/8/913:20
 * @版本:Ver1.0
 */

/**
 *上传文件工具类
 *@title:UpLoadUtils
 *@Author Eyucom-Qmd
 *@Date:2024/8/913:20
 *@Version:1.0
 */
public class UpLoadUtils {
    /*
    *上传方法
    * 返回上传文件的访问路径
    * 目录调用者创建
     */
    public  String uploadFile(MultipartFile file,String folder){
        try {
            //创建磁盘文件工厂
            WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
            ServletContext servletContext = webApplicationContext.getServletContext();
            String filePath=servletContext.getRealPath("/UpLoad/" + folder);
            String backPath=servletContext.getContextPath();
            //设置上传文件名称编码
            //处理上传的文件
            if(!file.isEmpty()){
                String fileName=file.getOriginalFilename();
                String suffixName = fileName.substring(fileName.lastIndexOf("."));
                fileName=new Date().getTime()+suffixName;
                String savePath=filePath+"\\"+fileName;
                // 检查文件夹是否在,不存在创建它
                File folders = new File(filePath);
                if (!folders.exists() && !folders.isDirectory()) {
                    folders.mkdirs();//mkdir只创建单级目录 mkdirs创建多级目录
                }
                file.transferTo(new File(savePath));
                return backPath+"/UpLoad/" + folder+"/"+fileName;
            }else{
                return "请选择需要上传的文件";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}

3.控制层处理Contrller

java 复制代码
 @RequestMapping("/upload")
    @ResponseBody
    public Map<String ,Object> upload(@RequestPart("upload") MultipartFile file)throws Exception{
//foldr 服务器中的分类目录文件夹
        String folder="ArtContenPhoto";
        Map<String,Object>imgMap=new HashMap<>();
//将返回来的路径添加到Map集合返回预览
       String imgURL=upLoadUtils.uploadFile(file,folder);
        imgMap.put("uploaded","1");
        imgMap.put("url",imgURL);
        return imgMap;
    }
相关推荐
sunshine22 girl18 分钟前
Java学习一 环境配置2 Idea的入门使用和maven配置,项目启动脚本配置
java·maven
有点。1 小时前
*C++哈夫曼树与哈夫曼编码
java·c++·servlet
CRMEB系统商城2 小时前
CRMEB标准版系统(Java)v3.1正式发布
java·spring·微信小程序·php·教育电商
写后端的胖头鱼2 小时前
【高频面试题】Java 基础类型 + 包装类 + String 互相转换
java·开发语言
weixin_460443562 小时前
企业考试系统从.NET迁移到Java+Linux:数据库迁移、接口兼容与灰度切换实践
java·煤矿培训考试系统·煤矿在线考试系统·煤矿安全培训·煤矿考试系统·一人一档
香菜TTT2 小时前
Redis的哨兵机制
java·数据库·redis
withoutfear3 小时前
IntelliJ IDEA卡顿内存分配不足和索引被频繁触发问题解决办法
java·ide·intellij-idea
大圣编蚕3 小时前
Java StringWriter 详解:从入门到实战
java·开发语言·python
行百里er3 小时前
Spring Insight 里收到的 Span 是怎么存下来的
spring boot·spring·监控
老苗项目实战4 小时前
Java工程师高频面试题(基于近一年的真实面试记录整理)
java·开发语言·面试·预见猿份·老苗项目实战