ResourceUtil.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.kmall.common.utils;
  2. import javax.servlet.http.HttpServletRequest;
  3. import java.io.UnsupportedEncodingException;
  4. import java.util.ResourceBundle;
  5. /**
  6. * 项目参数工具类
  7. * 项目参数各种操作方法集合类
  8. *
  9. * @author Scott
  10. * @email
  11. * @date 2017年10月28日 13:11:27
  12. */
  13. public final class ResourceUtil {
  14. private static ResourceUtil RESOURCE_UTIL = null;
  15. //private static ResourceBundle BUNDLE = ResourceBundle.getBundle("platform");
  16. private static ResourceBundle BUNDLE = null;
  17. private ResourceUtil() {
  18. }
  19. private static ResourceBundle getBundle(String propertiesName){
  20. return ResourceBundle.getBundle(propertiesName);
  21. }
  22. /**
  23. * 工厂实现配置文件读取
  24. *
  25. * @param properties 参数
  26. * @return ResourceUtil 工具类
  27. */
  28. public static ResourceUtil getInstance(String properties) {
  29. if (RESOURCE_UTIL == null) {
  30. RESOURCE_UTIL = new ResourceUtil();
  31. }
  32. if (properties != null) {
  33. BUNDLE = ResourceBundle.getBundle(properties);
  34. }
  35. return RESOURCE_UTIL;
  36. }
  37. /**
  38. * 工厂实现配置文件读取
  39. *
  40. * @return ResourceUtil
  41. */
  42. public static ResourceUtil getInstance() {
  43. if (RESOURCE_UTIL == null) {
  44. RESOURCE_UTIL = new ResourceUtil();
  45. }
  46. return RESOURCE_UTIL;
  47. }
  48. /**
  49. * 主要功能:获得请求路径
  50. * 注意事项:无
  51. *
  52. * @param request 请求
  53. * @return 请求路径
  54. */
  55. public static String getRequestPath(HttpServletRequest request) {
  56. String requestPath = request.getRequestURI() + "?"
  57. + request.getQueryString();
  58. if (requestPath.indexOf("&") > -1) {
  59. // 去掉其他参数
  60. requestPath = requestPath.substring(0, requestPath.indexOf("&"));
  61. }
  62. // 去掉项目路径
  63. requestPath = requestPath.substring(request.getContextPath().length() + 1);
  64. return requestPath;
  65. }
  66. /**
  67. * 主要功能:获取配置文件参数
  68. * 注意事项:无
  69. *
  70. * @param propertiesName 参数名称
  71. * @param name 参数名称
  72. * @return 参数名称对应值
  73. */
  74. public static String getConfigByName(String propertiesName, String name) {
  75. String value = "";
  76. try {
  77. value = new String(getBundle(propertiesName).getString(name).getBytes("iso8859-1"),
  78. "UTF-8");
  79. } catch (UnsupportedEncodingException e) {
  80. e.printStackTrace();
  81. }
  82. return value;
  83. }
  84. /**
  85. * 主要功能:获取配置文件参数
  86. * 注意事项:无
  87. *
  88. * @param name 参数名称
  89. * @return 参数名称对应值
  90. */
  91. public static String getConfigByName(String name) {
  92. String value = "";
  93. try {
  94. value = new String(BUNDLE.getString(name).getBytes("iso8859-1"),
  95. "UTF-8");
  96. } catch (UnsupportedEncodingException e) {
  97. e.printStackTrace();
  98. }
  99. return value;
  100. }
  101. /**
  102. * 主要功能:取得系统路径
  103. * 注意事项:无
  104. *
  105. * @return 系统路径
  106. */
  107. public static String getSysPath() {
  108. String path = Thread.currentThread().getContextClassLoader().getResource(
  109. "").toString();
  110. String temp = path.replaceFirst("file:/", "").replaceFirst(
  111. "WEB-INF/classes/", "");
  112. String separator = System.getProperty("file.separator");
  113. String resultPath = temp.replaceAll("/", separator + separator).replaceAll(
  114. "%20", " ");
  115. return resultPath;
  116. }
  117. /**
  118. * 主要功能:获取项目根目录
  119. * 注意事项:无
  120. *
  121. * @return 项目根目录
  122. */
  123. public static String getPorjectPath() {
  124. // 当前tomcat的bin目录的路径 如
  125. String nowpath;
  126. // apache-tomcat-6.0.14\bin
  127. String tempdir;
  128. nowpath = System.getProperty("user.dir");
  129. // 把bin 文件夹变到 webapps文件里面
  130. tempdir = nowpath.replace("bin", "webapps");
  131. // 拼成D:\java\software\apache-tomcat-6.0.14\webapps\sz_pro
  132. tempdir += "\\";
  133. return tempdir;
  134. }
  135. /**
  136. * 主要功能:取得ClassPath
  137. * 注意事项:无
  138. *
  139. * @return ClassPath
  140. */
  141. public static String getClassPath() {
  142. String path = Thread.currentThread().getContextClassLoader().getResource(
  143. "").toString();
  144. String temp = path.replaceFirst("file:/", "");
  145. String separator = System.getProperty("file.separator");
  146. String resultPath = temp.replaceAll("/", separator + separator);
  147. return resultPath;
  148. }
  149. /**
  150. * 主要功能:取得系统临时路径
  151. * 注意事项:无
  152. *
  153. * @return 系统临时路径
  154. */
  155. public static String getSystempPath() {
  156. return System.getProperty("java.io.tmpdir");
  157. }
  158. /**
  159. * 主要功能:取得分隔符
  160. * 注意事项:无
  161. *
  162. * @return 分隔符
  163. */
  164. public static String getSeparator() {
  165. return System.getProperty("file.separator");
  166. }
  167. /**
  168. * 主要功能:获取随机码的长度
  169. * 注意事项:无
  170. *
  171. * @return 随机码的长度
  172. */
  173. /*public static String getRandCodeLength() {
  174. return BUNDLE.getString("randCodeLength");
  175. }*/
  176. /**
  177. * 主要功能:获取随机码的类型
  178. * 注意事项:无
  179. *
  180. * @return 随机码的类型
  181. */
  182. /*public static String getRandCodeType() {
  183. return BUNDLE.getString("randCodeType");
  184. }*/
  185. /**
  186. * 主要功能:获取组织机构编码长度的类型
  187. * 注意事项:无
  188. *
  189. * @return 组织机构编码长度的类型
  190. */
  191. /*public static String getOrgCodeLengthType() {
  192. return BUNDLE.getString("orgCodeLengthType");
  193. }*/
  194. /**
  195. * 主要功能:获取数据库类型
  196. * 注意事项:无
  197. *
  198. * @return 数据库类型
  199. */
  200. public String getDbType() {
  201. return StringUtils.isNullOrEmpty(ResourceBundle.getBundle("platform").getString(
  202. "dbType")) ? "oracle" : ResourceBundle.getBundle("platform").getString(
  203. "dbType");
  204. }
  205. /**
  206. * 主要功能:获取数据库的schema,主要针对db2
  207. * 注意事项:无
  208. *
  209. * @return schema 字符串
  210. */
  211. public String getDbSchema() {
  212. String schema = "";
  213. if (StringUtils.isNotEmpty(ResourceBundle.getBundle("platform").getString(
  214. "dbType"))
  215. && "db2".equals(ResourceBundle.getBundle("platform").getString(
  216. "dbType"))) {
  217. schema = StringUtils.isNotEmpty(ResourceBundle.getBundle("platform").getString(
  218. "dbSchema")) ? ResourceBundle.getBundle("platform").getString(
  219. "dbSchema")
  220. + "." : "";
  221. }
  222. return schema;
  223. }
  224. }