解决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;
    }
相关推荐
学长毕业设计28 分钟前
基于springboot的云南中草药知识普及管理网站的设计与开发(源码+文档+讲解视频)
java·spring boot·后端
大模型码小白29 分钟前
AI 提示词专栏:使用系统指令(System Prompt)实现全局约束
java·大数据·运维·开发语言·人工智能·python·prompt
cxoptics36 分钟前
蓝宝石的轴向在窗片和偏振片中起的作用
java
wuminyu1 小时前
Java FFM处理网络读写事件源码剖析
java·linux·c语言·jvm·c++
小江的记录本1 小时前
【AI Agent】《2026年9月 AI Agent全栈开发技术选型专项面试宝典》
java·spring boot·python·spring·ai·面试·ai编程
梦梦代码精2 小时前
《LikeShop全产品技术硬核拆解:ThinkPHP8+Vue3+UniApp架构,私有化部署与二次开发实战指南》
java·低代码·系统架构·php·开源软件
一木 之林2 小时前
C/C++ 面向对象编程(OOP)(进阶)
java·c语言·c++
摇滚侠2 小时前
《SpringBoot 3:入门与应用实战》第 9 章 使用 WebMvc 开发应用 阅读笔记 2
java·spring boot·笔记
technology_x2 小时前
液冷超充站设备厂家哪家好?2026年功率与散热对比
java·开发语言
LayZhangStrive3 小时前
Agent开发 - MCP Client使用MCP Server的几种方式(Spring AI)
java·spring ai·mcp