ToolUtil.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. package com.huaxu.common;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.util.NumberUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.extra.spring.SpringUtil;
  7. import cn.hutool.system.SystemUtil;
  8. import cn.hutool.system.oshi.OshiUtil;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.context.MessageSource;
  11. import org.springframework.context.i18n.LocaleContextHolder;
  12. import java.io.*;
  13. import java.util.*;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;
  16. /**
  17. * 高频使用工具类
  18. * @author wyy
  19. * @date 2020-03-11 15:02
  20. */
  21. public class ToolUtil {
  22. private static int counter = 0;
  23. /**
  24. * 获取随机字符,自定义长度
  25. *
  26. * @author wyy
  27. * 2020-03-11 15:07
  28. */
  29. public static String getRandomString(int length) {
  30. String base = "abcdefghijklmnopqrstuvwxyz0123456789";
  31. Random random = new Random();
  32. StringBuffer sb = new StringBuffer();
  33. for (int i = 0; i < length; i++) {
  34. int number = random.nextInt(base.length());
  35. sb.append(base.charAt(number));
  36. }
  37. return sb.toString();
  38. }
  39. /**
  40. * 判断对象是否为空 true 不为空
  41. * @author wyy
  42. * @date 2020-03-11 15:07
  43. */
  44. public static boolean isNotEmpty(Object o) {
  45. return !isEmpty(o);
  46. }
  47. /**
  48. *
  49. * 对象是否为空 true 为空
  50. * @author wyy
  51. * @date 2020-03-11 15:09
  52. */
  53. public static boolean isEmpty(Object o) {
  54. if (o == null) {
  55. return true;
  56. }
  57. if (o instanceof String) {
  58. if ("".equals(o.toString().trim())) {
  59. return true;
  60. }
  61. } else if (o instanceof List) {
  62. if (((List<?>) o).size() == 0) {
  63. return true;
  64. }
  65. } else if (o instanceof Map) {
  66. if (((Map<?, ?>) o).size() == 0) {
  67. return true;
  68. }
  69. } else if (o instanceof Set) {
  70. if (((Set<?>) o).size() == 0) {
  71. return true;
  72. }
  73. } else if (o instanceof Object[]) {
  74. if (((Object[]) o).length == 0) {
  75. return true;
  76. }
  77. } else if (o instanceof int[]) {
  78. if (((int[]) o).length == 0) {
  79. return true;
  80. }
  81. } else if (o instanceof long[]) {
  82. if (((long[]) o).length == 0) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. /**
  89. * 对象组中是否存在空对象
  90. *
  91. * @author wyy
  92. * @Date 2020-03-11 15:09
  93. */
  94. public static boolean isOneEmpty(Object... os) {
  95. for (Object o : os) {
  96. if (isEmpty(o)) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * 对象组中是否全是空对象
  104. *
  105. * @author wyy
  106. * @date 2020-03-11 15:09
  107. */
  108. public static boolean isAllEmpty(Object... os) {
  109. for (Object o : os) {
  110. if (!isEmpty(o)) {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. /**
  117. * 根据消息键和参数 获取消息 委托给Spring messageSource
  118. *
  119. * @param code 消息键
  120. * @param args 参数
  121. * @return 获取国际化翻译值
  122. */
  123. public static String message(String code, Object... args){
  124. MessageSource messageSource = SpringUtil.getBean(MessageSource.class);
  125. try{
  126. return messageSource.getMessage(code, args, LocaleContextHolder.getLocale());
  127. }catch (Exception e){
  128. return code;
  129. }
  130. }
  131. /**
  132. *
  133. * 字节计算转换
  134. *
  135. * <pre>
  136. * StrUtil.convertFileSize(1024) = 1kB
  137. * </pre>
  138. * @author wyy
  139. * @time 2019-04-03 12:29
  140. * @param size 字节大小
  141. * @return 转换后大小字符串
  142. *
  143. */
  144. public static String convertFileSize(long size) {
  145. long kb = 1024;
  146. long mb = kb * 1024;
  147. long gb = mb * 1024;
  148. if (size >= gb)
  149. {
  150. return StrUtil.format("{} GB", NumberUtil.round((float) size / gb,2));
  151. }
  152. else if (size >= mb)
  153. {
  154. float f = NumberUtil.round((float) size / mb,2).floatValue();
  155. return StrUtil.format(f > 100 ? "{} MB" : "{} MB", f);
  156. }
  157. else if (size >= kb)
  158. {
  159. float f = NumberUtil.round((float) size / kb,2).floatValue();
  160. return StrUtil.format(f > 100 ? "{} KB" : "{} KB", f);
  161. }
  162. else
  163. {
  164. return StrUtil.format("{} B", size);
  165. }
  166. }
  167. public static String getMessage(Exception e){
  168. StringWriter sw = null;
  169. PrintWriter pw = null;
  170. try {
  171. sw = new StringWriter();
  172. pw = new PrintWriter(sw);
  173. e.printStackTrace(pw);
  174. pw.flush();
  175. sw.flush();
  176. } finally {
  177. if (sw != null) {
  178. try {
  179. sw.close();
  180. } catch (IOException e1) {
  181. e1.printStackTrace();
  182. }
  183. }
  184. if (pw != null) {
  185. pw.close();
  186. }
  187. }
  188. return sw.toString();
  189. }
  190. public static boolean isBoolIp(String ipAddress) {
  191. String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
  192. Pattern pattern = Pattern.compile(ip);
  193. Matcher matcher = pattern.matcher(ipAddress);
  194. return matcher.matches();
  195. }
  196. public static String createFolder(String folder){
  197. folder += File.separator + DateUtil.format(new Date(),"yyyy") + File.separator + DateUtil.format(new Date(),"MM") +
  198. File.separator + DateUtil.format(new Date(),"dd")+ File.separator;
  199. FileUtil.mkdir(folder);
  200. return folder;
  201. }
  202. /**
  203. * 编码文件名
  204. */
  205. public static String encodingFilename(String fileName) {
  206. fileName = fileName.replace("_", " ");
  207. fileName = Md5Util.hash(fileName + System.nanoTime() + counter++);
  208. return fileName;
  209. }
  210. public static String encodingExcelFilename(String filename){
  211. filename = UUID.randomUUID().toString() + "_" + filename + ".xlsx";
  212. return filename;
  213. }
  214. /**
  215. * 修正路径,将 \\ 或 / 等替换为 File.separator
  216. * @param path 待修正的路径
  217. * @return 修正后的路径
  218. */
  219. public static String path(String path){
  220. String p = StringUtils.replace(path, "\\", "/");
  221. p = StringUtils.join(StringUtils.split(p, "/"), "/");
  222. if (!StringUtils.startsWithAny(p, "/") && StringUtils.startsWithAny(path, "\\", "/")){
  223. p += "/";
  224. }
  225. if (!StringUtils.endsWithAny(p, "/") && StringUtils.endsWithAny(path, "\\", "/")){
  226. p = p + "/";
  227. }
  228. if (path != null && path.startsWith("/")){
  229. p = "/" + p; // linux下路径
  230. }
  231. return p;
  232. }
  233. /**
  234. * 获取工程源文件所在路径
  235. * @return
  236. */
  237. public static String getProjectPath(){
  238. String projectPath = "";
  239. try {
  240. File file = ResourceUtil.getResource("").getFile();
  241. if (file != null){
  242. while(true){
  243. File f = new File(path(file.getPath() + "/src/main"));
  244. if (f.exists()){
  245. break;
  246. }
  247. f = new File(path(file.getPath() + "/target/classes"));
  248. if (f.exists()){
  249. break;
  250. }
  251. if (file.getParentFile() != null){
  252. file = file.getParentFile();
  253. }else{
  254. break;
  255. }
  256. }
  257. projectPath = file.toString();
  258. }
  259. } catch (FileNotFoundException e) {
  260. // 忽略异常
  261. } catch (IOException e) {
  262. // 忽略异常
  263. }
  264. // 取不到,取当前工作路径
  265. if (StringUtils.isBlank(projectPath)){
  266. projectPath = System.getProperty("user.dir");
  267. }
  268. return projectPath;
  269. }
  270. /**
  271. * 获取工程源文件所在路径
  272. * @return
  273. */
  274. public static String getWebappPath(){
  275. String webappPath = "";
  276. try {
  277. File file = ResourceUtil.getResource("").getFile();
  278. if (file != null){
  279. while(true){
  280. File f = new File(path(file.getPath() + "/WEB-INF/classes"));
  281. if (f.exists()){
  282. break;
  283. }
  284. f = new File(path(file.getPath() + "/src/main/webapp"));
  285. if (f.exists()){
  286. return f.getPath();
  287. }
  288. if (file.getParentFile() != null){
  289. file = file.getParentFile();
  290. }else{
  291. break;
  292. }
  293. }
  294. webappPath = file.toString();
  295. }
  296. } catch (FileNotFoundException e) {
  297. // 忽略异常
  298. } catch (IOException e) {
  299. // 忽略异常
  300. }
  301. // 取不到,取当前工作路径
  302. if (StringUtils.isBlank(webappPath)){
  303. webappPath = System.getProperty("user.dir");
  304. }
  305. return webappPath;
  306. }
  307. /**
  308. * 数组以某种分隔符拼装
  309. * @param value Long数值
  310. * @param s 分隔符
  311. * @return 拼装之后的字符串
  312. */
  313. public static String conversion(Object value, String s){
  314. String src = "";
  315. if(value instanceof Long[]){
  316. Long[] a = (Long[]) value;
  317. for(Long l: a){
  318. src += l+s;
  319. }
  320. }
  321. if(value instanceof List){
  322. List<String> a = (List) value;
  323. for(int i=0; i< a.size(); i++){
  324. src += a.get(i)+s;
  325. }
  326. }
  327. return src.substring(0,src.length()-s.length());
  328. }
  329. }