项目中使用UEditor发现设置图片自定义保存路径会出现《请求后台配置项http错误,上传功能将不能正常使用!错误》

 /* 上传图片配置项 */
"imageActionName": "uploadimage", /* 执行上传图片的action名称 */
"imageFieldName": "inputForm", /* 提交的图片表单名称 */
"imageMaxSize": , /* 上传大小限制,单位B */
"imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
"imageCompressEnable": true, /* 是否压缩图片,默认是true */
"imageCompressBorder": , /* 图片压缩最长边限制 */
"imageInsertAlign": "none", /* 插入的图片浮动方式 */
"imageUrlPrefix": "/cms/static/userfiles/", /* 图片访问路径前缀 */
"imagePathFormat": "/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */

原因在于UEditor提供的 ueditor-1.1.2.jar 之中的 ConfigManager 类获取不到 config.json 文件

上传图片会获取controller.jsp地址

 //ueditor.config.js

var URL = window.UEDITOR_HOME_URL || getUEBasePath();
/**
* 配置项主体。注意,此处所有涉及到路径的配置别遗漏URL变量。
*/
window.UEDITOR_CONFIG = { //为编辑器实例添加一个路径,这个不能被注释
UEDITOR_HOME_URL: URL // 服务器统一请求接口路径
, serverUrl: URL + "jsp/controller.jsp"

执行controller.jsp,rootPath 是要保存图片的路径

//controller.jsp
<% request.setCharacterEncoding( "utf-8" );
response.setHeader("Content-Type" , "text/html"); String rootPath = application.getRealPath( "/" );
out.write( new ActionEnter( request, rootPath ).exec() ); %>
修改后
<%

    request.setCharacterEncoding("utf-8");
response.setHeader("Content-Type", "text/html");
// String rootPath = application.getRealPath( "/" );
String rootPath = Global.getConfig("userfiles.basedir");//图片保存目录,会与imagePathFormat拼接起来,此为主要保存路径,imagePathFormat可以只设置图片名称
String jsonPath = Global.getConfig("userfiles.jsonPash"); //config.json 目录
out.write(new ActionEnter(request, rootPath, jsonPath).exec());
%>

ActionEnter执行了 ConfigManager.getInstance 传入了 request.getRequestURI()
request.getRequestURI()获取的是 controller.jsp文件的相对路径
 
//ActionEnter类
public ActionEnter(HttpServletRequest request, String rootPath) {
this.request = request;
this.rootPath = rootPath;
this.actionType = request.getParameter("action");
this.contextPath = request.getContextPath();
this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, request.getRequestURI());
}

修改后

 public ActionEnter(HttpServletRequest request, String rootPath, String jsonPath) {
this.request = request;
this.rootPath = rootPath;
this.actionType = request.getParameter("action");
this.contextPath = request.getContextPath();
this.configManager = ConfigManager.getInstance(this.rootPath, this.contextPath, jsonPath);
}

此时 ConfigManager 方法用你的图片保存路径加上 controller.jsp 的相对路径去找 config.json,就出现找不到的情况,可以修改jar包,把 request.getRequestURI()更改为变量,以传参方式把地址传进来,再把if去掉,直接使用传 uri 给 File


//ConfigManager类
public final class ConfigManager {
private final String rootPath;
private final String originalPath;
private final String contextPath;
private static final String configFileName = "config.json";
private String parentPath = null;
private JSONObject jsonConfig = null;
private static final String SCRAWL_FILE_NAME = "scrawl";
private static final String REMOTE_FILE_NAME = "remote";
  
private ConfigManager(String rootPath, String contextPath, String uri) throws FileNotFoundException, IOException {
rootPath = rootPath.replace("\\", "/");
this.rootPath = rootPath;
this.contextPath = contextPath;
if (contextPath.length() > ) {
this.originalPath = this.rootPath + uri.substring(contextPath.length());
} else {
this.originalPath = this.rootPath + uri;
} this.initEnv();
} public static ConfigManager getInstance(String rootPath, String contextPath, String uri) {
try {
return new ConfigManager(rootPath, contextPath, uri);
} catch (Exception var4) {
return null;
}
}
  //获取 config.json
private void initEnv() throws FileNotFoundException, IOException {
File file = new File(this.originalPath);
if (!file.isAbsolute()) {
file = new File(file.getAbsolutePath());
} this.parentPath = file.getParent();
String configContent = this.readFile(this.getConfigPath()); try {
JSONObject jsonConfig = new JSONObject(configContent);
this.jsonConfig = jsonConfig;
} catch (Exception var4) {
this.jsonConfig = null;
} }
  //获取 config.json 路径
private String getConfigPath() {
return this.parentPath + File.separator + "config.json";
}
}

修改后

private ConfigManager(String rootPath, String contextPath, String jsonPath) throws FileNotFoundException, IOException {
rootPath = rootPath.replace("\\", "/");
this.rootPath = rootPath;
this.contextPath = contextPath;
this.originalPath = jsonPath;
this.initEnv();
}

图片上传成功

百度UEditor富文本上传图片的更多相关文章

  1. ASP.NET MVC5 中百度ueditor富文本编辑器的使用

    随着网站信息发布内容越来越多,越来越重视美观,富文本编辑就是不可缺少的了,众多编辑器比较后我选了百度的ueditor富文本编辑器. 百度ueditor富文本编辑器分为两种一种是完全版的ueditor, ...

  2. 百度ueditor富文本编辑器的使用

    百度ueditor富文本编辑器的使用 //以下为我在官网下载的ueditor v1.3.5 php版的大楷配置步骤第一步: //配置文件的引入应该比功能文件先引入,最后设置语言类型.即:editor. ...

  3. PHP如何搭建百度Ueditor富文本编辑器

    本文为大家分享了PHP搭建百度Ueditor富文本编辑器的方法,供大家参考,具体内容如下 下载UEditor 官网:下载地址 将下载好的文件解压到thinkphp项目中,本文是解压到PUBLIC目录下 ...

  4. django之百度Ueditor富文本编辑器后台集成

    Python3 + Django2.0 百度Ueditor 富文本编辑器的集成 百度富文本编辑器官网地址:http://fex.baidu.com/ueditor/ 疑问:为什么要二次集成? 答案:因 ...

  5. ueditor富文本上传图片的时候报错"未找上传数据"

    最近因为需求所以在ssh项目中使用了Ueditor富文本插件,但是在上传图片的时候总是提示“未找到上传数据”,之后百度了好久终于弄明白了.因为Ueditor在上传图片的时候会访问controller. ...

  6. spring boot 整合 百度ueditor富文本

    百度的富文本没有提供Java版本的,只给提供了jsp版本,但是呢spring boot 如果是使用内置tomcat启动的话整合jsp是非常困难得,今天小编给大家带来spring boot整合百度富文本 ...

  7. vue集成百度UEditor富文本编辑器

    在前端开发的项目中.难免会遇到需要在页面上集成一个富文本编辑器.那么.如果你有这个需求.希望可以帮助到你 vue是前端开发者所追捧的框架,简单易上手,但是基于vue的富文本编辑器大多数太过于精简.于是 ...

  8. 百度UEditor富文本插件的使用

    这个富文本还是功能挺全的. 官方文档地址 下载地址 常用接口 较完整代码仓库 UEditor下载后直接运行即可访问,但在上传文件时需要单独再做配置. [很详细的SpringBoot整合UEditor教 ...

  9. 百度UEditor(富文本编辑器)的基础用法

    百度的这个编辑器挺强大的,这里只是用他的文本功能,没有介绍上传图片视频的. 我用是的SSH来写的项目. 1. 把下载的UEditor(ueditor1_4_3_1-utf8-jsp)解压后全部复制到W ...

随机推荐

  1. P1478 陶陶摘苹果(升级版)

    题目描述 又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果,这次她有一个a公分的椅子.当他手够不着时,他会站到椅子上再试试. 这次与NOIp2005普及组第一题不同的是:陶陶之前搬凳子,力 ...

  2. QT入门学习

    第一个QT程序 #include<QApplication> #include<QDialog> #include<QLabel> #include<QTex ...

  3. Objective-C Memory Management Being Exceptional 异常处理与内存

    Objective-C Memory Management    Being Exceptional  异常处理与内存 3.1Cocoa requires that all exceptions mu ...

  4. Python学习 Day 4 函数 切片 迭代 列表生成式 生成器

    定义函数 def my_abs(x):#求绝对值的my_abs函数 if x >= 0: return x else: return –x def nop():#空函数 pass#占位符 参数检 ...

  5. sql创建发送邮件账号

    ALTER PROCEDURE dbo.sx_pro_autoEmail_account AS begin Declare @my_profile_name varchar(50) Declare @ ...

  6. WPF学习- AllowDrop 用户控件启用拖放功能

    知识点: 创建自定义用户控件(UserControl) 使用户控件成为拖动源 使用户控件成为放置目标 使面板能够接收从用户控件放置的数据 创建项目: 1.新建WPF项目(Wpf-AllowDrop) ...

  7. vue项目打包步骤及运行打包项目

    (1)项目打包 终端运行命令 npm run build 打包成功的标志与项目的改变,如下图: 点击index.html,通过浏览器运行,出现以下报错,如图: 那么应该如何修改呢?    具体步骤如下 ...

  8. P2347 砝码称重(动态规划递推,背包,洛谷)

    题目链接:P2347 砝码称重 参考题解:点击进入 纪念我第一道没理解题意的题 ''但不包括一个砝码也不用的情况'',这句话我看成了每个砝码起码放一个 然后就做不出来了 思路: 1.这题数据很小,10 ...

  9. LBE_登录Demo

    目录 服务器 最小资产库创建 entity配置 实体的Python实现 创建第一个空间Space 让entity进入空间Space 客户端(unity) 生成客户端SDK 实现Client部分验证 验 ...

  10. 小DEMO之manifest初体验

    前言 补漏洞系列~今天来动手体验一下HTML5中的离线应用之mainifest缓存清单.实际上H5还提供了一个JavaScript接口来用于更新缓存文件的方法以及对缓存文件的操作.在Chrome中,输 ...