123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- package com.kmall.common.utils;
- import javax.servlet.http.HttpServletRequest;
- import java.io.UnsupportedEncodingException;
- import java.util.ResourceBundle;
- /**
- * 项目参数工具类
- * 项目参数各种操作方法集合类
- *
- * @author Scott
- * @email
- * @date 2017年10月28日 13:11:27
- */
- public final class ResourceUtil {
- private static ResourceUtil RESOURCE_UTIL = null;
- //private static ResourceBundle BUNDLE = ResourceBundle.getBundle("platform");
- private static ResourceBundle BUNDLE = null;
- private ResourceUtil() {
- }
- private static ResourceBundle getBundle(String propertiesName){
- return ResourceBundle.getBundle(propertiesName);
- }
- /**
- * 工厂实现配置文件读取
- *
- * @param properties 参数
- * @return ResourceUtil 工具类
- */
- public static ResourceUtil getInstance(String properties) {
- if (RESOURCE_UTIL == null) {
- RESOURCE_UTIL = new ResourceUtil();
- }
- if (properties != null) {
- BUNDLE = ResourceBundle.getBundle(properties);
- }
- return RESOURCE_UTIL;
- }
- /**
- * 工厂实现配置文件读取
- *
- * @return ResourceUtil
- */
- public static ResourceUtil getInstance() {
- if (RESOURCE_UTIL == null) {
- RESOURCE_UTIL = new ResourceUtil();
- }
- return RESOURCE_UTIL;
- }
- /**
- * 主要功能:获得请求路径
- * 注意事项:无
- *
- * @param request 请求
- * @return 请求路径
- */
- public static String getRequestPath(HttpServletRequest request) {
- String requestPath = request.getRequestURI() + "?"
- + request.getQueryString();
- if (requestPath.indexOf("&") > -1) {
- // 去掉其他参数
- requestPath = requestPath.substring(0, requestPath.indexOf("&"));
- }
- // 去掉项目路径
- requestPath = requestPath.substring(request.getContextPath().length() + 1);
- return requestPath;
- }
- /**
- * 主要功能:获取配置文件参数
- * 注意事项:无
- *
- * @param propertiesName 参数名称
- * @param name 参数名称
- * @return 参数名称对应值
- */
- public static String getConfigByName(String propertiesName, String name) {
- String value = "";
- try {
- value = new String(getBundle(propertiesName).getString(name).getBytes("iso8859-1"),
- "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return value;
- }
- /**
- * 主要功能:获取配置文件参数
- * 注意事项:无
- *
- * @param name 参数名称
- * @return 参数名称对应值
- */
- public static String getConfigByName(String name) {
- String value = "";
- try {
- value = new String(BUNDLE.getString(name).getBytes("iso8859-1"),
- "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return value;
- }
- /**
- * 主要功能:取得系统路径
- * 注意事项:无
- *
- * @return 系统路径
- */
- public static String getSysPath() {
- String path = Thread.currentThread().getContextClassLoader().getResource(
- "").toString();
- String temp = path.replaceFirst("file:/", "").replaceFirst(
- "WEB-INF/classes/", "");
- String separator = System.getProperty("file.separator");
- String resultPath = temp.replaceAll("/", separator + separator).replaceAll(
- "%20", " ");
- return resultPath;
- }
- /**
- * 主要功能:获取项目根目录
- * 注意事项:无
- *
- * @return 项目根目录
- */
- public static String getPorjectPath() {
- // 当前tomcat的bin目录的路径 如
- String nowpath;
- // apache-tomcat-6.0.14\bin
- String tempdir;
- nowpath = System.getProperty("user.dir");
- // 把bin 文件夹变到 webapps文件里面
- tempdir = nowpath.replace("bin", "webapps");
- // 拼成D:\java\software\apache-tomcat-6.0.14\webapps\sz_pro
- tempdir += "\\";
- return tempdir;
- }
- /**
- * 主要功能:取得ClassPath
- * 注意事项:无
- *
- * @return ClassPath
- */
- public static String getClassPath() {
- String path = Thread.currentThread().getContextClassLoader().getResource(
- "").toString();
- String temp = path.replaceFirst("file:/", "");
- String separator = System.getProperty("file.separator");
- String resultPath = temp.replaceAll("/", separator + separator);
- return resultPath;
- }
- /**
- * 主要功能:取得系统临时路径
- * 注意事项:无
- *
- * @return 系统临时路径
- */
- public static String getSystempPath() {
- return System.getProperty("java.io.tmpdir");
- }
- /**
- * 主要功能:取得分隔符
- * 注意事项:无
- *
- * @return 分隔符
- */
- public static String getSeparator() {
- return System.getProperty("file.separator");
- }
- /**
- * 主要功能:获取随机码的长度
- * 注意事项:无
- *
- * @return 随机码的长度
- */
- /*public static String getRandCodeLength() {
- return BUNDLE.getString("randCodeLength");
- }*/
- /**
- * 主要功能:获取随机码的类型
- * 注意事项:无
- *
- * @return 随机码的类型
- */
- /*public static String getRandCodeType() {
- return BUNDLE.getString("randCodeType");
- }*/
- /**
- * 主要功能:获取组织机构编码长度的类型
- * 注意事项:无
- *
- * @return 组织机构编码长度的类型
- */
- /*public static String getOrgCodeLengthType() {
- return BUNDLE.getString("orgCodeLengthType");
- }*/
- /**
- * 主要功能:获取数据库类型
- * 注意事项:无
- *
- * @return 数据库类型
- */
- public String getDbType() {
- return StringUtils.isNullOrEmpty(ResourceBundle.getBundle("platform").getString(
- "dbType")) ? "oracle" : ResourceBundle.getBundle("platform").getString(
- "dbType");
- }
- /**
- * 主要功能:获取数据库的schema,主要针对db2
- * 注意事项:无
- *
- * @return schema 字符串
- */
- public String getDbSchema() {
- String schema = "";
- if (StringUtils.isNotEmpty(ResourceBundle.getBundle("platform").getString(
- "dbType"))
- && "db2".equals(ResourceBundle.getBundle("platform").getString(
- "dbType"))) {
- schema = StringUtils.isNotEmpty(ResourceBundle.getBundle("platform").getString(
- "dbSchema")) ? ResourceBundle.getBundle("platform").getString(
- "dbSchema")
- + "." : "";
- }
- return schema;
- }
- }
|