慢慢来比较快,虚心学技术
前言:前后端分离的项目,需要使用ueditor富文本编辑器,因为是未接触过的前后端分离,所以遇到了新的问题
问题:因为UEditor需要与后端代码结合,即将配置文件config.json放在服务器端访问,但是项目运行前端无法上传图片,报后端配置文件初始化失败的错误,config.json如下
问题分析:前后端分离的项目中前端无权直接访问后端静态资源,导致UEditor无法读取配置文件config.json
解决方案:将配置文件config.json放在src/main/resource中,然后重写UEditor.jar的源码,前端通过访问后端上传接口,读取配置文件(替代直接读取静态文件的方式)
1. 反编译UEditor.jar的源码,得到代码结构如下:
2.摘录其中最重要的代码片段-读取配置文件:
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;
// 涂鸦上传filename定义
private final static String SCRAWL_FILE_NAME = "scrawl";
// 远程图片抓取filename定义
private final static String REMOTE_FILE_NAME = "remote";
/*
* 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件
*/
private ConfigManager(String rootPath, String contextPath, String uri) throws FileNotFoundException, IOException {
rootPath = rootPath.replace("\\", "/");
this.rootPath = rootPath;
this.contextPath = contextPath;
this.originalPath = "src/main/resources/config.json";
this.initEnv();
}
/**
* 配置管理器构造工厂
*
* @param rootPath
* 服务器根路径
* @param contextPath
* 服务器所在项目路径
* @param uri
* 当前访问的uri
* @return 配置管理器实例或者null
*/
public static ConfigManager getInstance(String rootPath, String contextPath, String uri) {
try {
return new ConfigManager(rootPath, contextPath, uri);
} catch (Exception e) {
return null;
}
}
// 验证配置文件加载是否正确
public boolean valid() {
return this.jsonConfig != null;
}
public JSONObject getAllConfig() {
return this.jsonConfig;
}
public Map getConfig(int type) throws JSONException {
Map conf = new HashMap();
String savePath = null;
String localSavePathPrefix = null;
switch (type) {
case ActionMap.UPLOAD_FILE:
conf.put("isBase64", "false");
conf.put("maxSize", this.jsonConfig.getLong("fileMaxSize"));
conf.put("allowFiles", this.getArray("fileAllowFiles"));
conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));
savePath = this.jsonConfig.getString("filePathFormat");
break;
case ActionMap.UPLOAD_IMAGE:
conf.put("isBase64", "false");
conf.put("maxSize", this.jsonConfig.getLong("imageMaxSize"));
conf.put("allowFiles", this.getArray("imageAllowFiles"));
conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));
// 指定文件根路径,该值从application中获取,并传送过来
conf.put("rootPath", this.rootPath);
savePath = this.jsonConfig.getString("imagePathFormat");
localSavePathPrefix = this.jsonConfig.getString("localSavePathPrefix");
break;
case ActionMap.UPLOAD_VIDEO:
conf.put("maxSize", this.jsonConfig.getLong("videoMaxSize"));
conf.put("allowFiles", this.getArray("videoAllowFiles"));
conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));
savePath = this.jsonConfig.getString("videoPathFormat");
localSavePathPrefix = this.jsonConfig.getString("localSavePathPrefix");
break;
case ActionMap.UPLOAD_SCRAWL:
conf.put("filename", ConfigManager.SCRAWL_FILE_NAME);
conf.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"));
conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
conf.put("isBase64", "true");
savePath = this.jsonConfig.getString("scrawlPathFormat");
break;
case ActionMap.CATCH_IMAGE:
conf.put("filename", ConfigManager.REMOTE_FILE_NAME);
conf.put("filter", this.getArray("catcherLocalDomain"));
conf.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"));
conf.put("allowFiles", this.getArray("catcherAllowFiles"));
conf.put("fieldName", this.jsonConfig.getString("catcherFieldName") + "[]");
savePath = this.jsonConfig.getString("catcherPathFormat");
localSavePathPrefix = this.jsonConfig.getString("localSavePathPrefix");
break;
case ActionMap.LIST_IMAGE:
conf.put("allowFiles", this.getArray("imageManagerAllowFiles"));
conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));
conf.put("count", this.jsonConfig.getInteger("imageManagerListSize"));
break;
case ActionMap.LIST_FILE:
conf.put("allowFiles", this.getArray("fileManagerAllowFiles"));
conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));
conf.put("count", this.jsonConfig.getInteger("fileManagerListSize"));
break;
}
conf.put("savePath", savePath);
conf.put("localSavePathPrefix", localSavePathPrefix);
conf.put("rootPath", this.rootPath);
return conf;
}
private void initEnv() throws FileNotFoundException, IOException {
File file = new File(this.originalPath);
if (!file.isAbsolute()) {
file = new File(file.getAbsolutePath());
}
// 判断是否为本地环境,如果为本地环境,则需要更换json文件的读取路径
if (file.toString().contains(":\\")) {
file = new File("src/main/resources/config.json");
if (!file.isAbsolute()) {
file = new File(file.getAbsolutePath());
}
}
this.parentPath = file.getParent();
// String configContent = this.readFile( this.getConfigPath() );
String configContent = this.getConfigContent();
// 更改获取配置文件的方式
// String configPath =
// ResourceUtils.getFile("classpath:config.json").getAbsolutePath();
// String configContent = this.readFile( configPath );
try {
JSONObject jsonConfig = JSON.parseObject(configContent);
this.jsonConfig = jsonConfig;
} catch (Exception e) {
this.jsonConfig = null;
}
}
private String getConfigPath() {
return this.parentPath + File.separator + ConfigManager.configFileName;
}
private String[] getArray(String key) throws JSONException {
JSONArray jsonArray = this.jsonConfig.getJSONArray(key);
String[] result = new String[jsonArray.size()];
for (int i = 0, len = jsonArray.size(); i < len; i++) {
result[i] = jsonArray.getString(i);
}
return result;
}
private String readFile(String path) throws IOException {
StringBuilder builder = new StringBuilder();
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8");
BufferedReader bfReader = new BufferedReader(reader);
String tmpContent = null;
while ((tmpContent = bfReader.readLine()) != null) {
builder.append(tmpContent);
}
bfReader.close();
} catch (UnsupportedEncodingException e) {
// 忽略
}
return this.filter(builder.toString());
}
private String getConfigContent() {
InputStream in = this.getClass().getResourceAsStream("/config.json");
String result = null;
try {
StringBuffer sb = new StringBuffer();
byte[] b = new byte[1024];
int length = 0;
while (-1 != (length = in.read(b))) {
sb.append(new String(b, 0, length, "utf-8"));
}
result = sb.toString().replaceAll("/\\*(.|[\\r\\n])*?\\*/", "");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
// 过滤输入字符串, 剔除多行注释以及替换掉反斜杠
private String filter(String input) {
return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
}
}
3.转接的controller则需要自己去写:
@RequestMapping(value = "/ueditor/exec")
@ResponseBody
public String exec(HttpServletRequest request) throws UnsupportedEncodingException {
request.setCharacterEncoding("utf-8");
String rootPath = config.getUploadRoot();
logger.info("rootPath:" + rootPath);
return new ActionEnter(request, rootPath).exec();
}
4.使用Postman自己去访问http://localhost:端口号/ueditor/exec?action=config&&noCache=1543741521627时,返回对应于config.json的内容,则说明配置文件读取成功了,剩下的事情就是前后端对接了
问题解决,解决方案参考了csdn内的大神文章,但是当时没有记录,找不到文章了,纯属笔记,如有冒犯,还请见谅
如有贻误,还请评论指出