CommonUtil.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package com.yihu.wlyy.util;
  2. import java.io.*;
  3. import com.fasterxml.jackson.databind.node.ObjectNode;
  4. import com.yihu.wlyy.config.FastDFSConfig;
  5. import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
  6. import org.apache.commons.io.FileUtils;
  7. import org.apache.commons.lang3.StringUtils;
  8. public class CommonUtil {
  9. /**
  10. * 获取图片全路径
  11. *
  12. * @return
  13. */
  14. public static String getPhoneUrl(String url) {
  15. if (StringUtils.isEmpty(url)) {
  16. return "";
  17. } else {
  18. if (url.indexOf("http") > -1) {
  19. return url;
  20. } else {
  21. return SystemConf.getInstance().getServerUrl() + url;
  22. }
  23. }
  24. }
  25. public static String getMobileEncode(String mobile) {
  26. if (StringUtils.isNotEmpty(mobile) && mobile.length() == 11) {
  27. return mobile.substring(0, 4) + "****" + mobile.substring(8, 11);
  28. }
  29. return mobile;
  30. }
  31. public static String getIdcardEncode(String idcard) {
  32. if (idcard != null) {
  33. if (idcard.length() == 18) {
  34. return idcard.substring(0, 9) + "*******" + idcard.substring(16, 18);
  35. } else if (idcard.length() == 15) {
  36. return idcard.substring(0, 8) + "***" + idcard.substring(11, 15);
  37. }
  38. }
  39. return idcard;
  40. }
  41. /**
  42. * 对象转数组
  43. *
  44. * @param obj
  45. * @return
  46. */
  47. public static byte[] toByteArray(Object obj) {
  48. byte[] bytes = null;
  49. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  50. try {
  51. ObjectOutputStream oos = new ObjectOutputStream(bos);
  52. oos.writeObject(obj);
  53. oos.flush();
  54. bytes = bos.toByteArray();
  55. oos.close();
  56. bos.close();
  57. } catch (IOException ex) {
  58. ex.printStackTrace();
  59. }
  60. return bytes;
  61. }
  62. /**
  63. * 数组转对象
  64. *
  65. * @param bytes
  66. * @return
  67. */
  68. public static Object toObject(byte[] bytes) {
  69. Object obj = null;
  70. try {
  71. ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  72. ObjectInputStream ois = new ObjectInputStream(bis);
  73. obj = ois.readObject();
  74. ois.close();
  75. bis.close();
  76. } catch (IOException ex) {
  77. ex.printStackTrace();
  78. } catch (ClassNotFoundException ex) {
  79. ex.printStackTrace();
  80. }
  81. return obj;
  82. }
  83. public static final InputStream byte2Input(byte[] buf) {
  84. return new ByteArrayInputStream(buf);
  85. }
  86. public static final byte[] input2byte(InputStream inStream) throws IOException {
  87. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
  88. byte[] buff = new byte[100];
  89. int rc = 0;
  90. while ((rc = inStream.read(buff, 0, 100)) > 0) {
  91. swapStream.write(buff, 0, rc);
  92. }
  93. byte[] in2b = swapStream.toByteArray();
  94. return in2b;
  95. }
  96. /**
  97. * 返回:张三为张*
  98. *
  99. * @param name 姓名
  100. * @return
  101. */
  102. public static String getEncryptName(String name) {
  103. if (StringUtils.isEmpty(name)) {
  104. return null;
  105. }
  106. if (name.length() == 1) {
  107. return name;
  108. }
  109. // 获取姓
  110. String temp = name.substring(0, 1);
  111. for (int i = 1; i < name.length(); i++) {
  112. temp += "*";
  113. }
  114. return temp;
  115. }
  116. /**
  117. * 拷贝临时图片到存储目录
  118. *
  119. * @param images
  120. * @return
  121. * @throws IOException
  122. */
  123. public static String copyTempImage(String images) throws IOException {
  124. // 文件保存的临时路径
  125. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  126. // 图片保存路径
  127. String imagePath = SystemConf.getInstance().getImagePath() + File.separator;
  128. String[] tempImages = images.split(",");
  129. String temp = "";
  130. for (String image : tempImages) {
  131. File file = new File(tempPath + image);
  132. File smallFile = new File(tempPath + image + "_small");
  133. if (file.exists() && smallFile.exists()) {
  134. // 原图拷贝路径
  135. File targetFile = new File(imagePath + image);
  136. // 缩略图拷贝路径
  137. File targetSmallFile = new File(imagePath + image + "_small");
  138. // 拷贝原图
  139. FileUtils.copyFile(file, targetFile);
  140. // 拷贝缩略图
  141. FileUtils.copyFile(smallFile, targetSmallFile);
  142. // 删除临时文件
  143. FileUtils.forceDelete(file);
  144. FileUtils.forceDelete(smallFile);
  145. if (temp.length() == 0) {
  146. temp = SystemConf.getInstance().getImageServer() + image;
  147. } else {
  148. temp += "," + SystemConf.getInstance().getImageServer() + image;
  149. }
  150. }
  151. }
  152. return temp;
  153. }
  154. /**
  155. * 拷贝临时语音文件到存储目录
  156. *
  157. * @param voices
  158. * @return
  159. * @throws IOException
  160. */
  161. public static String copyTempVoice(String voices) throws IOException {
  162. // 文件保存的临时路径
  163. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  164. // 语音保存路径
  165. String voicePath = SystemConf.getInstance().getVoicePath() + File.separator;
  166. String[] tempVoices = voices.split(",");
  167. String temp = "";
  168. for (String voice : tempVoices) {
  169. File file = new File(tempPath + voice);
  170. if (file.exists()) {
  171. // 目标文件
  172. File targetFile = new File(voicePath + voice);
  173. // 拷贝到指定路径
  174. FileUtils.copyFile(file, targetFile);
  175. // 删除临时文件
  176. FileUtils.forceDelete(file);
  177. if (temp.length() == 0) {
  178. temp = SystemConf.getInstance().getVoiceServer() + voice;
  179. } else {
  180. temp += "," + SystemConf.getInstance().getVoiceServer() + voice;
  181. }
  182. }
  183. }
  184. return temp;
  185. }
  186. /**
  187. * 校验健康指标是否正常
  188. *
  189. * @param curValue 当前值
  190. * @param standardMax 最大标准值
  191. * @param standardMin 最小标准值
  192. * @return 0正常,1高,-1低
  193. */
  194. public static double checkHealthIndex(double curValue, double standardMax, double standardMin) {
  195. if (curValue <= 0) {
  196. return 0;
  197. }
  198. if (standardMax > 0 && curValue > standardMax) {
  199. return curValue;
  200. }
  201. // 低于标准值,暂不推
  202. // if (standardMin > 0 && curValue < standardMin) {
  203. // return -curValue;
  204. // }
  205. return 0;
  206. }
  207. public static String copyTempFilesToFastDFS(String files) throws Exception {
  208. // 文件保存的临时路径
  209. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  210. String[] fileArray = files.split(",");
  211. FastDFSUtil fastDFSUtil = new FastDFSConfig().fastDFSUtil();
  212. String fileUrls = "";
  213. for (String file : fileArray) {
  214. File f = new File(tempPath + file);
  215. if (f.exists()) {
  216. String fileName = f.getName();
  217. InputStream in = new FileInputStream(f);
  218. ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
  219. in.close();
  220. if (result != null) {
  221. fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + result.get("fileUrl").toString().replaceAll("\"","");
  222. }
  223. }
  224. }
  225. return fileUrls;
  226. }
  227. }