项目中使用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. HTML5 File API的应用

    HTML5 File API简介 HTML5File API协议族 Directories and System   文件系统和目录读取 FileWriter   写入文件 FileReader   ...

  2. Django系列:(1)PyCharm下创建并运行我们的第一个Django工程

    准备工作: 假设读者已经安装好python 2x或3x,以及安装好Django,以及Pycharm. 我的配置: – Python 2.7.11 – Pycharm Professional 5.0. ...

  3. git ---回到过去

    git命令回顾 git checkout /git reset -git reset HEAD~    //~代表回滚到第几个版本.. 有多个的话可以在~后面加个数字 git reset --mixe ...

  4. R Programming week2 Functions and Scoping Rules

    A Diversion on Binding Values to Symbol When R tries to bind a value to a symbol,it searches through ...

  5. 解决重置PostgreSQL 9.6密码的问题

    一.PostgreSql9.6重置密码的方法: 1.打开windows服务管理器,找到“postgresql-x64-9.6”服务,停止服务. 2.找到PostgreSQL9.6的安装目录(以我的E盘 ...

  6. BotFramework学习-02

    1.请求的Message格式 { "type": "Message", "id": "fd89606f8014453ca5587e ...

  7. 解决docker pull镜像速度慢的问题

    直接下载Docker镜像时,由于种种原因,经常下载失败,即使连接成功也是很慢,怎么办呢 目前我知道可以提升速度的办法:DaoCloud 提供Docker Hub Mirror服务 用户可以进入Dock ...

  8. hdfs深入:06、hdfs的写入过程

    7.HDFS的文件写入过程 详细步骤解析: 1. client发起文件上传请求,通过RPC与NameNode建立通讯,NameNode检查目标文件是否已存在,父目录是否存在,返回是否可以上传: 2. ...

  9. 「 HDU 1978 」 How many ways

    # 解题思路 记忆化搜索 一个点可以跳到的点,取决于它现在的能量.而且有一个显而易见的性质就是一条可行路径的终点和起点的横坐标之差加上纵坐标之差肯定小于等于起点的能量. 因为跳到一个点之后,能量和之前 ...

  10. GNU编译器学习 --> 如何链接外部库【Linking with external libraries】

    库也就是我们常说的library,一个库是若干个已经编译过的目标文件(.obj)的集合,它可以被链接到程序里.那么我们最常见的使用就是,我们在编程时会调用一些函数,这些函数别人已经写好了,它就放在库里 ...