ueditor编辑器java使用_java/springboot整合UEditor编辑器

本文介绍了如何在Spring Boot项目中整合ueditor编辑器,包括解决配置路径问题、确保config.json配置正确加载以及自定义上传路径。通过调整服务器统一请求接口路径,设置Controller以返回配置信息,确保图片、视频等上传功能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网下载jsp版ueditor放进项目,其实放在哪个目录看项目习惯(有些按照网上的来就是路径问题导致),主要是获取配置的时候能找到相应的路径。

d6a6b14fde03958aa0ba349ca9559173.png

页面上引入相应js,加上也可script方式

ueditor加载会去读取:服务器统一请求接口路径,默认为:serverUrl: URL + "jsp/controller.jsp"

加载时首先会以get方式去读取配置信息(jsp/config.json里面的内容),然后再以post请求去上传图片或视频(请求后面带的参数会不一样【get】http://xxxx/xx?action=config或者【post】http://xxxx/xx?action=uploadimage)

get请求不到的话会报:请求后台配置项http错误,上传功能将不能正常使用!

59ab068458ef8f4a182fa861b6143f82.png

get请求到了但返回配置错误的话上传会出错:

0304df6be5aa1c18a405139273de9932.png

需要注意的是jsp版本的,而使用的是spring boot,请求应该经过控制器,而不能直接去访问这个jsp,当我们直接在浏览器输入这个请求http://xxxx/ueditor/jsp/controller.jsp?action=config&&noCache=12342时你会发现,这成了一个下载链接,可以下载contoller.jsp。

所以注意如果不修改serverUrl: URL + "jsp/controller.jsp"那一定要保证controller.jsp能正常访问且返回config.json里面的配置。

70b873de6909d9f5dab9d00e65ddfde0.png

返回格式一定需要是这样:

a4e50c854ee77891e9cc45c59c4ed4d3.png

在此提供两种方式获取config配置:

1.继续使用读取js/config.json目录里的配置,修改ueditor.config.js中配置的服务器统一请求接口路径serverUrl的值去后台执行方法获取,如下:

8687356f23a671b8d292a139fcbd1b90.png

后台方法(方法名自定义):

e0b3c35c1d483128638eb3942347e4c0.png

这个rootPath是指向的是config.json所在的目录(static/js/plugins/ueditor/jsp),spring boot中应该这样修改才能获取的到,然后用PrintWriter来输出json格式的配置信息。那么这个方法就作为了ueditor向服务器发送请求的控制器了,从而取代了controller.jsp的作用

这个controller可以用现有的,也可新建。但一定要注意这个请求一定是http://xxxx/config不能是http://xxxx/file(或其他)/config,代码如下:

@Controller

public class Config {

@RequestMapping(value="/config")

public void config(HttpServletRequest request, HttpServletResponse response) {

response.setContentType("application/json");

String rootPath = ClassUtils.getDefaultClassLoader().getResource("").getPath()+"static/js/plugins/ueditor/jsp";

System.out.println(rootPath);

try {

String exec = new ActionEnter(request, rootPath).exec();

System.out.println(exec);

PrintWriter writer = response.getWriter();

writer.write(exec);

writer.flush();

writer.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

而不应该是这样:

92b3afc15cf0912b10e624474c65ca6a.png

后台方法还有一层/file

79bb7814d6158b2ed09dff63db86bf9b.png

如果确要如此,必须满足下面获取路径的条件。

因为跟踪请求方法看:

c9c705604490ecb919800db21bbac326.png

ActionEnter方法里面有一个request.getRequestURI():获取请求的地址链接(浏览器中输入的地址)与getConfigPath()【this.parentPath + File.separator + "config.json"】(会取上一层路径)

且继续执行会有:

1dc7f01676f2a955f1510e04794f69ab.png

efd42237cd1225c4226271d916571d59.png

结果:

rootPath:/target/classes/static/js/plugins/ueditor/jsp

uri:/config

originalPath:/target/classes/static/js/plugins/ueditor/jsp/config

5bd33306961b426f7cd30d0914aeb992.png

getConfigPath方法就能正确获取到config.json地址:/target/classes/static/js/plugins/ueditor/jsp/config.json

而如果是 /file/config那获取到的路径:/target/classes/static/js/plugins/ueditor/jsp/file/config.json此路径就会错误。

用此方式相应的当选择了图片post提交时候先判断action

9b3fdf1fa001230b0b2c445380a740eb.png

UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;

UE.Editor.prototype.getActionUrl = function(action) {

// 如果触发了下面三个动作中,则进行文件上传操作

if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {

return Tools.ctxPath + '/file/uploadimage';

} else {

return this._bkGetActionUrl.call(this, action);

}

}

以此来完成自定义路径上传(当然方法可统一一个,根据action来判断执行,如下)

2.不使用jsp/controller.jsp文件,直接从服务器处获取配置文件:

3b8abc0371a51354471efe6f4822d68c.png

方法:

@RequestMapping("/uploadimage")

@ResponseBody

public Object uploadimage(@RequestParam(name = "action", required = false) String action,@RequestParam(name = "upfile", required = false) MultipartFile upfile, HttpServletRequest request) {

JSONObject object = new JSONObject();

if (action != null && action.equals("config")) {//获取配置文件

return JSONUtil.parse(UEditorUtil.UEDITOR_CONFIG);

} else if (action != null && action.equals("uploadimage")) {//直接自定义上传:oss

if (upfile != null) {

//{state:”数据状态信息”,url:”图片回显路径”,title:”文件title”,original:”文件名称”}

try {

return ossUtil.uploadimage(upfile);

} catch (Exception e) {

e.printStackTrace();

logger.error(e);

object.put("state", "err");

return object;

}

} else {

object.put("state", "文件为空");

return object;

}

} else {

object.put("state", "不支持该操作");

return object;

}

}

UEditorUtil帮助类:(把config.json里面的配置直接写成json字符串进行返回)

public class UEditorUtil {

public final static String UEDITOR_CONFIG = "{" +

"\"imageActionName\": \"uploadimage\"," +

"\"imageFieldName\": \"upfile\"," +

"\"imageMaxSize\": 2048000," +

"\"imageAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"]," +

"\"imageCompressEnable\": true," +

"\"imageCompressBorder\": 1600," +

"\"imageInsertAlign\": \"none\"," +

"\"imageUrlPrefix\": \"\"," +

"\"imagePathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\"," +

"" +

"\"scrawlActionName\": \"uploadscrawl\"," +

"\"scrawlFieldName\": \"upfile\"," +

"\"scrawlPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\"," +

"\"scrawlMaxSize\": 2048000," +

"\"scrawlUrlPrefix\": \"\"," +

"\"scrawlInsertAlign\": \"none\"," +

"" +

"\"snapscreenActionName\": \"uploadimage\"," +

"\"snapscreenPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\"," +

"\"snapscreenUrlPrefix\": \"\"," +

"\"snapscreenInsertAlign\": \"none\"," +

"" +

"\"catcherLocalDomain\": [\"127.0.0.1\", \"localhost\", \"img.baidu.com\"]," +

"\"catcherActionName\": \"catchimage\"," +

"\"catcherFieldName\": \"source\"," +

"\"catcherPathFormat\": \"/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}\"," +

"\"catcherUrlPrefix\": \"\"," +

"\"catcherMaxSize\": 2048000," +

"\"catcherAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"]," +

"" +

"\"videoActionName\": \"uploadvideo\"," +

"\"videoFieldName\": \"upfile\"," +

"\"videoPathFormat\": \"/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}\"," +

"\"videoUrlPrefix\": \"\"," +

"\"videoMaxSize\": 102400000," +

"\"videoAllowFiles\": [" +

"\".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\"," +

"\".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\"]," +

"" +

"\"fileActionName\": \"uploadfile\"," +

"\"fileFieldName\": \"upfile\"," +

"\"filePathFormat\": \"/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}\"," +

"\"fileUrlPrefix\": \"\"," +

"\"fileMaxSize\": 51200000," +

"\"fileAllowFiles\": [" +

"\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"," +

"\".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\"," +

"\".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\"," +

"\".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\"," +

"\".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"" +

"]," +

"" +

"\"imageManagerActionName\": \"listimage\"," +

"\"imageManagerListPath\": \"/ueditor/jsp/upload/image/\"," +

"\"imageManagerListSize\": 20," +

"\"imageManagerUrlPrefix\": \"\"," +

"\"imageManagerInsertAlign\": \"none\"," +

"\"imageManagerAllowFiles\": [\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"]," +

"" +

"\"fileManagerActionName\": \"listfile\"," +

"\"fileManagerListPath\": \"/ueditor/jsp/upload/file/\"," +

"\"fileManagerUrlPrefix\": \"\"," +

"\"fileManagerListSize\": 20," +

"\"fileManagerAllowFiles\": [" +

"\".png\", \".jpg\", \".jpeg\", \".gif\", \".bmp\"," +

"\".flv\", \".swf\", \".mkv\", \".avi\", \".rm\", \".rmvb\", \".mpeg\", \".mpg\"," +

"\".ogg\", \".ogv\", \".mov\", \".wmv\", \".mp4\", \".webm\", \".mp3\", \".wav\", \".mid\"," +

"\".rar\", \".zip\", \".tar\", \".gz\", \".7z\", \".bz2\", \".cab\", \".iso\"," +

"\".doc\", \".docx\", \".xls\", \".xlsx\", \".ppt\", \".pptx\", \".pdf\", \".txt\", \".md\", \".xml\"" +

"] " +

"" +

"}";

/**

* Ueditor的返回状态类型

*/

public enum UeditorMsg {

SUCCESS("SUCCESS"), ERROR("上传失败");

private String v;

UeditorMsg(String v) {

this.v = v;

}

public String get() {

return this.v;

}

}

}

注意上传成功后的返回格式:

JSONObject object=new JSONObject();

object.put("url", "");//这个url是前台回显路径(回显路径为config.json中的imageUrlPrefix+此处的url)

object.put("state", "SUCCESS");

object.put("original", "");

object.put("title", "");

最后具体怎样引入依赖项-百度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值