首页>代码>Spring MVC+mybatis+easyui+ueditor+shiro权限框架开发吉客多后台管理系统>/geekdol/src/com/baidu/ueditor/ConfigManager.java
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; import com.baidu.ueditor.define.ActionMap; /** * 配置管理器 * * @author hancong03@baidu.com * */ public final class ConfigManager { private final String rootPath; private final String originalPath; private final String contextPath; private final String saveRootPath; private static final String geekstoreConfigFileName = "geekstoreConfig.json"; private static final String hbridgeConfigFileName = "hbridgeConfig.json"; private static final String hmediaConfigFileName = "hmediaConfig.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"; private String type; // 判断加载哪个 .json文件 /* * 通过一个给定的路径构建一个配置管理器, 该管理器要求地址路径所在目录下必须存在config.properties文件 */ private ConfigManager(String rootPath, String saveRootPath, String contextPath, String uri, String type) throws FileNotFoundException, IOException { this.type = type; rootPath = rootPath.replace("\\", "/"); this.rootPath = rootPath; this.contextPath = contextPath; this.saveRootPath = saveRootPath; if (contextPath.length() > 0) { this.originalPath = this.rootPath + uri.substring(contextPath.length()); } else { this.originalPath = this.rootPath + uri; } this.initEnv(); } /** * 配置管理器构造工厂 * * @param rootPath * 服务器根路径 * @param contextPath * 服务器所在项目路径 * @param uri * 当前访问的uri * @return 配置管理器实例或者null */ public static ConfigManager getInstance(String rootPath, String saveRootPath, String contextPath, String uri, String type) { try { return new ConfigManager(rootPath, saveRootPath, contextPath, uri, type); } 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, Object>(); String savePath = 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")); savePath = this.jsonConfig.getString("imagePathFormat"); 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"); 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"); break; case ActionMap.LIST_IMAGE: conf.put("allowFiles", this.getArray("imageManagerAllowFiles")); conf.put("dir", this.jsonConfig.getString("imageManagerListPath")); conf.put("count", this.jsonConfig.getInt("imageManagerListSize")); break; case ActionMap.LIST_FILE: conf.put("allowFiles", this.getArray("fileManagerAllowFiles")); conf.put("dir", this.jsonConfig.getString("fileManagerListPath")); conf.put("count", this.jsonConfig.getInt("fileManagerListSize")); break; } conf.put("savePath", savePath); conf.put("saveRootPath", this.saveRootPath); conf.put("rootPath", this.rootPath); // 远程图片抓取前缀 conf.put("zhuaquQianzhui", this.jsonConfig.getString("catcherUrlPrefix")); 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 = this.readFile(this.getConfigPath()); try { JSONObject jsonConfig = new JSONObject(configContent); this.jsonConfig = jsonConfig; } catch (Exception e) { this.jsonConfig = null; } } private String getConfigPath() { if (type.equals("geekstore")) { return this.parentPath + File.separator + ConfigManager.geekstoreConfigFileName; } else if (type.equals("hbridge")) { return this.parentPath + File.separator + ConfigManager.hbridgeConfigFileName; } else { return this.parentPath + File.separator + ConfigManager.hmediaConfigFileName; } } private String[] getArray(String key) { JSONArray jsonArray = this.jsonConfig.getJSONArray(key); String[] result = new String[jsonArray.length()]; for (int i = 0, 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 e) { // 忽略 } return this.filter(builder.toString()); } // 过滤输入字符串, 剔除多行注释以及替换掉反斜杠 private String filter(String input) { return input.replaceAll("/\\*[\\s\\S]*?\\*/", ""); } }
最近下载更多
testuser1234567 LV24
2022年7月21日
1900110734 LV6
2021年4月14日
zjjhzjb LV14
2021年3月27日
1587767925 LV7
2020年12月24日
qq331974234 LV3
2020年12月17日
123456nty LV37
2020年12月15日
sunlea LV20
2020年12月6日
iven_muyi LV8
2020年10月12日
起名字是个麻烦事 LV12
2020年7月7日
1229290378 LV8
2020年7月6日
最近浏览更多
wddq123
4月3日
暂无贡献等级
xuweiwowzy LV5
1月14日
WBelong LV8
2023年12月26日
矿泉水 LV30
2023年8月10日
shaoqi
2023年7月26日
暂无贡献等级
lzr369258147 LV1
2023年6月9日
1379585889 LV11
2023年6月7日
yueeeii
2023年4月21日
暂无贡献等级
yuanshun LV6
2023年4月4日
uni-code_0123 LV1
2023年3月23日