1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 百度编辑器上传图片自定义路径 访问路径动态加载

百度编辑器上传图片自定义路径 访问路径动态加载

时间:2018-11-03 03:00:02

相关推荐

百度编辑器上传图片自定义路径 访问路径动态加载

在用百度编辑器时发现他以前的上传图片只能上传到项目的根目录下 这叫人很郁闷,在网上找了一些资料,现在记录一下。

1.首先在config.json中添加root路径,这里我起名叫uploadRoot字段。

"uploadRoot":"/home/data",

2.然后将imagePathFormat字段修改成自己的上传路径。

"imagePathFormat": "/upload/{yyyy}{mm}{dd}/image/{time}{rand:6}",

3.在ueditor-1.1.2.jar包中找到ConfigManager.class类,反编译后代码如下:

package com.baidu.ueditor;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Map;import org.json.JSONArray;import org.json.JSONObject;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() > 0) {this.originalPath = (this.rootPath + uri.substring(contextPath.length()));} else {this.originalPath = (this.rootPath + uri);}initEnv();}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<String, Object> getConfig(int type){Map<String, Object> conf = new HashMap();String savePath = null;switch (type){case 4: conf.put("isBase64", "false");conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("fileMaxSize")));conf.put("allowFiles", getArray("fileAllowFiles"));conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));savePath = this.jsonConfig.getString("filePathFormat");break;case 1: conf.put("isBase64", "false");conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("imageMaxSize")));conf.put("allowFiles", getArray("imageAllowFiles"));conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));savePath = this.jsonConfig.getString("imagePathFormat");break;case 3: conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("videoMaxSize")));conf.put("allowFiles", getArray("videoAllowFiles"));conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));savePath = this.jsonConfig.getString("videoPathFormat");break;case 2: conf.put("filename", "scrawl");conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("scrawlMaxSize")));conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));conf.put("isBase64", "true");savePath = this.jsonConfig.getString("scrawlPathFormat");break;case 5: conf.put("filename", "remote");conf.put("filter", getArray("catcherLocalDomain"));conf.put("maxSize", Long.valueOf(this.jsonConfig.getLong("catcherMaxSize")));conf.put("allowFiles", getArray("catcherAllowFiles"));conf.put("fieldName", this.jsonConfig.getString("catcherFieldName") + "[]");savePath = this.jsonConfig.getString("catcherPathFormat");break;case 7: conf.put("allowFiles", getArray("imageManagerAllowFiles"));conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));conf.put("count", Integer.valueOf(this.jsonConfig.getInt("imageManagerListSize")));break;case 6: conf.put("allowFiles", getArray("fileManagerAllowFiles"));conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));conf.put("count", Integer.valueOf(this.jsonConfig.getInt("fileManagerListSize")));}conf.put("savePath", savePath);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());}this.parentPath = file.getParent();String configContent = readFile(getConfigPath());try{JSONObject jsonConfig = new JSONObject(configContent);this.jsonConfig = jsonConfig;}catch (Exception e){this.jsonConfig = null;}}private String getConfigPath(){return this.parentPath + File.separator + "config.json";}private String[] getArray(String key){JSONArray jsonArray = this.jsonConfig.getJSONArray(key);String[] result = new String[jsonArray.length()];int i = 0;for (int len = jsonArray.length(); 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 localUnsupportedEncodingException) {}return filter(builder.toString());}private String filter(String input){return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");}}

4.找到 conf.put("rootPath", this.rootPath) 这一行代码,将其修改成从配置文件读取的uploadRoot字段

conf.put("rootPath", this.jsonConfig.getString("uploadRoot"));

这样就配置好了,可以上传到自己配置的目录下了。

5.还有读取图片时imageUrlPrefix字段只能配置一个路径,但是真正开发时候有开发环境 ,还有生产环境,太麻烦。可以在ueditor.all.js中添加getNowHost方法:

function getNowHost(){var host = window.location.host;return host;}link = "http://" + getNowHost() + me.options.imageUrlPrefix + json.url;

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。