CommonUtil.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package com.yihu.wlyy.util;
  2. import java.io.*;
  3. import java.util.Date;
  4. import java.util.Random;
  5. import com.fasterxml.jackson.databind.node.ObjectNode;
  6. import com.yihu.wlyy.config.FastDFSConfig;
  7. import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
  8. import it.sauronsoftware.jave.*;
  9. import org.apache.commons.io.FileUtils;
  10. import org.apache.commons.lang3.StringUtils;
  11. public class CommonUtil {
  12. /**
  13. * 获取图片全路径
  14. *
  15. * @return
  16. */
  17. public static String getPhoneUrl(String url) {
  18. if (StringUtils.isEmpty(url)) {
  19. return "";
  20. } else {
  21. if (url.indexOf("http") > -1) {
  22. return url;
  23. } else {
  24. return SystemConf.getInstance().getServerUrl() + url;
  25. }
  26. }
  27. }
  28. public static String getMobileEncode(String mobile) {
  29. if (StringUtils.isNotEmpty(mobile) && mobile.length() == 11) {
  30. return mobile.substring(0, 4) + "****" + mobile.substring(8, 11);
  31. }
  32. return mobile;
  33. }
  34. public static String getIdcardEncode(String idcard) {
  35. if (idcard != null) {
  36. if (idcard.length() == 18) {
  37. return idcard.substring(0, 9) + "*******" + idcard.substring(16, 18);
  38. } else if (idcard.length() == 15) {
  39. return idcard.substring(0, 8) + "***" + idcard.substring(11, 15);
  40. }
  41. }
  42. return idcard;
  43. }
  44. /**
  45. * 对象转数组
  46. *
  47. * @param obj
  48. * @return
  49. */
  50. public static byte[] toByteArray(Object obj) {
  51. byte[] bytes = null;
  52. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  53. try {
  54. ObjectOutputStream oos = new ObjectOutputStream(bos);
  55. oos.writeObject(obj);
  56. oos.flush();
  57. bytes = bos.toByteArray();
  58. oos.close();
  59. bos.close();
  60. } catch (IOException ex) {
  61. ex.printStackTrace();
  62. }
  63. return bytes;
  64. }
  65. /**
  66. * 数组转对象
  67. *
  68. * @param bytes
  69. * @return
  70. */
  71. public static Object toObject(byte[] bytes) {
  72. Object obj = null;
  73. try {
  74. ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  75. ObjectInputStream ois = new ObjectInputStream(bis);
  76. obj = ois.readObject();
  77. ois.close();
  78. bis.close();
  79. } catch (IOException ex) {
  80. ex.printStackTrace();
  81. } catch (ClassNotFoundException ex) {
  82. ex.printStackTrace();
  83. }
  84. return obj;
  85. }
  86. public static final InputStream byte2Input(byte[] buf) {
  87. return new ByteArrayInputStream(buf);
  88. }
  89. public static final byte[] input2byte(InputStream inStream) throws IOException {
  90. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
  91. byte[] buff = new byte[100];
  92. int rc = 0;
  93. while ((rc = inStream.read(buff, 0, 100)) > 0) {
  94. swapStream.write(buff, 0, rc);
  95. }
  96. byte[] in2b = swapStream.toByteArray();
  97. return in2b;
  98. }
  99. /**
  100. * 返回:张三为张*
  101. *
  102. * @param name 姓名
  103. * @return
  104. */
  105. public static String getEncryptName(String name) {
  106. if (StringUtils.isEmpty(name)) {
  107. return null;
  108. }
  109. if (name.length() == 1) {
  110. return name;
  111. }
  112. // 获取姓
  113. String temp = name.substring(0, 1);
  114. for (int i = 1; i < name.length(); i++) {
  115. temp += "*";
  116. }
  117. return temp;
  118. }
  119. /**
  120. * 拷贝临时图片到存储目录
  121. *
  122. * @param images
  123. * @return
  124. * @throws IOException
  125. */
  126. // public static String copyTempImage(String images) throws IOException {
  127. // // 文件保存的临时路径
  128. // String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  129. // // 图片保存路径
  130. // String imagePath = SystemConf.getInstance().getImagePath() + File.separator;
  131. // String[] tempImages = images.split(",");
  132. // String temp = "";
  133. // for (String image : tempImages) {
  134. // File file = new File(tempPath + image);
  135. // File smallFile = new File(tempPath + image + "_small");
  136. // if (file.exists() && smallFile.exists()) {
  137. // // 原图拷贝路径
  138. // File targetFile = new File(imagePath + image);
  139. // // 缩略图拷贝路径
  140. // File targetSmallFile = new File(imagePath + image + "_small");
  141. // // 拷贝原图
  142. // FileUtils.copyFile(file, targetFile);
  143. // // 拷贝缩略图
  144. // FileUtils.copyFile(smallFile, targetSmallFile);
  145. // // 删除临时文件
  146. // FileUtils.forceDelete(file);
  147. // FileUtils.forceDelete(smallFile);
  148. // if (temp.length() == 0) {
  149. // temp = SystemConf.getInstance().getImageServer() + image;
  150. // } else {
  151. // temp += "," + SystemConf.getInstance().getImageServer() + image;
  152. // }
  153. // }
  154. // }
  155. // return temp;
  156. // }
  157. /**
  158. * 拷贝临时语音文件到存储目录
  159. *
  160. * @param voices
  161. * @return
  162. * @throws IOException
  163. */
  164. public static String copyTempVoice(String voices) throws Exception {
  165. // 文件保存的临时路径
  166. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  167. String serverUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
  168. FastDFSUtil fastDFSUtil = new FastDFSUtil();
  169. String fileUrls = "";
  170. File f = new File(tempPath + voices);
  171. if (f.exists()) {
  172. String fileName = f.getName();
  173. InputStream in = new FileInputStream(f);
  174. ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
  175. in.close();
  176. if (result != null) {
  177. fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
  178. + result.get("groupName").toString().replaceAll("\"", "") + "/"
  179. + result.get("remoteFileName").toString().replaceAll("\"", "");
  180. f.delete();
  181. }
  182. }
  183. return fileUrls;
  184. }
  185. /**
  186. * 校验健康指标是否正常
  187. *
  188. * @param curValue 当前值
  189. * @param standardMax 最大标准值
  190. * @param standardMin 最小标准值
  191. * @return 0正常,1高,-1低
  192. */
  193. public static double checkHealthIndex(double curValue, double standardMax, double standardMin) {
  194. if (curValue <= 0) {
  195. return 0;
  196. }
  197. if (standardMax > 0 && curValue > standardMax) {
  198. return curValue;
  199. }
  200. // 低于标准值,暂不推
  201. // if (standardMin > 0 && curValue < standardMin) {
  202. // return -curValue;
  203. // }
  204. return 0;
  205. }
  206. /**
  207. * 上传文件到FastDFS
  208. *
  209. * @param files 临时文件
  210. * @return
  211. * @throws Exception
  212. */
  213. public static String copyTempImage(String files) throws Exception {
  214. // 文件保存的临时路径
  215. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  216. String serverUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
  217. String[] fileArray = files.split(",");
  218. FastDFSUtil fastDFSUtil = new FastDFSUtil();
  219. String fileUrls = "";
  220. for (String file : fileArray) {
  221. File f = new File(tempPath + file);
  222. File fs = new File(tempPath + file + "_small");
  223. if (f.exists()) {
  224. String fileName = f.getName();
  225. InputStream in = new FileInputStream(f);
  226. ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
  227. in.close();
  228. if (result != null) {
  229. fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
  230. + result.get("groupName").toString().replaceAll("\"", "") + "/"
  231. + result.get("remoteFileName").toString().replaceAll("\"", "");
  232. f.delete();
  233. if (fs.exists()) {
  234. fs.delete();
  235. }
  236. }
  237. }
  238. }
  239. return fileUrls;
  240. }
  241. public static void changeToMp3(String sourcePath, String targetPath) {
  242. File source = new File(sourcePath);
  243. File target = new File(targetPath);
  244. AudioAttributes audio = new AudioAttributes();
  245. Encoder encoder = new Encoder();
  246. audio.setCodec("libmp3lame");
  247. EncodingAttributes attrs = new EncodingAttributes();
  248. attrs.setFormat("mp3");
  249. attrs.setAudioAttributes(audio);
  250. try {
  251. encoder.encode(source, target, attrs);
  252. } catch (IllegalArgumentException e) {
  253. e.printStackTrace();
  254. } catch (InputFormatException e) {
  255. e.printStackTrace();
  256. } catch (EncoderException e) {
  257. e.printStackTrace();
  258. }
  259. }
  260. public static String saveVoiceToDisk(InputStream inputStream,String newFileName) throws Exception {
  261. // 文件保存的临时路径
  262. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  263. // 拼接年月日路径
  264. String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
  265. // 保存路径
  266. File uploadFile = new File(tempPath + datePath + newFileName);
  267. FileOutputStream fileOutputStream = null;
  268. try {
  269. if (!uploadFile.getParentFile().exists()) {
  270. uploadFile.getParentFile().mkdirs();
  271. }
  272. byte[] data = new byte[1024];
  273. int len = 0;
  274. fileOutputStream = new FileOutputStream(uploadFile);
  275. while ((len = inputStream.read(data)) != -1) {
  276. fileOutputStream.write(data, 0, len);
  277. }
  278. // 返回保存路径
  279. return tempPath+datePath + newFileName;
  280. } catch (IOException e) {
  281. e.printStackTrace();
  282. } finally {
  283. if (inputStream != null) {
  284. try {
  285. inputStream.close();
  286. } catch (IOException e) {
  287. e.printStackTrace();
  288. }
  289. }
  290. if (fileOutputStream != null) {
  291. try {
  292. fileOutputStream.close();
  293. } catch (IOException e) {
  294. e.printStackTrace();
  295. }
  296. }
  297. }
  298. return null;
  299. }
  300. }