CommonUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. package com.yihu.wlyy.util;
  2. import com.fasterxml.jackson.databind.node.ObjectNode;
  3. import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
  4. import it.sauronsoftware.jave.*;
  5. import org.apache.commons.lang3.StringUtils;
  6. import java.io.*;
  7. public class CommonUtil {
  8. /**
  9. * 获取图片全路径
  10. *
  11. * @return
  12. */
  13. public static String getPhoneUrl(String url) {
  14. if (StringUtils.isEmpty(url)) {
  15. return "";
  16. } else {
  17. if (url.indexOf("http") > -1) {
  18. return url;
  19. } else {
  20. return SystemConf.getInstance().getServerUrl() + url;
  21. }
  22. }
  23. }
  24. public static String getMobileEncode(String mobile) {
  25. if (StringUtils.isNotEmpty(mobile) && mobile.length() == 11) {
  26. return mobile.substring(0, 4) + "****" + mobile.substring(8, 11);
  27. }
  28. return mobile;
  29. }
  30. public static String getIdcardEncode(String idcard) {
  31. if (idcard != null) {
  32. if (idcard.length() == 18) {
  33. return idcard.substring(0, 9) + "*******" + idcard.substring(16, 18);
  34. } else if (idcard.length() == 15) {
  35. return idcard.substring(0, 8) + "***" + idcard.substring(11, 15);
  36. }
  37. }
  38. return idcard;
  39. }
  40. /**
  41. * 对象转数组
  42. *
  43. * @param obj
  44. * @return
  45. */
  46. public static byte[] toByteArray(Object obj) {
  47. byte[] bytes = null;
  48. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  49. try {
  50. ObjectOutputStream oos = new ObjectOutputStream(bos);
  51. oos.writeObject(obj);
  52. oos.flush();
  53. bytes = bos.toByteArray();
  54. oos.close();
  55. bos.close();
  56. } catch (IOException ex) {
  57. ex.printStackTrace();
  58. }
  59. return bytes;
  60. }
  61. /**
  62. * 数组转对象
  63. *
  64. * @param bytes
  65. * @return
  66. */
  67. public static Object toObject(byte[] bytes) {
  68. Object obj = null;
  69. try {
  70. ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
  71. ObjectInputStream ois = new ObjectInputStream(bis);
  72. obj = ois.readObject();
  73. ois.close();
  74. bis.close();
  75. } catch (IOException ex) {
  76. ex.printStackTrace();
  77. } catch (ClassNotFoundException ex) {
  78. ex.printStackTrace();
  79. }
  80. return obj;
  81. }
  82. public static final InputStream byte2Input(byte[] buf) {
  83. return new ByteArrayInputStream(buf);
  84. }
  85. public static final byte[] input2byte(InputStream inStream) throws IOException {
  86. ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
  87. byte[] buff = new byte[100];
  88. int rc = 0;
  89. while ((rc = inStream.read(buff, 0, 100)) > 0) {
  90. swapStream.write(buff, 0, rc);
  91. }
  92. byte[] in2b = swapStream.toByteArray();
  93. return in2b;
  94. }
  95. /**
  96. * 返回:张三为张*
  97. *
  98. * @param name 姓名
  99. * @return
  100. */
  101. public static String getEncryptName(String name) {
  102. if (StringUtils.isEmpty(name)) {
  103. return null;
  104. }
  105. if (name.length() == 1) {
  106. return name;
  107. }
  108. // 获取姓
  109. String temp = name.substring(0, 1);
  110. for (int i = 1; i < name.length(); i++) {
  111. temp += "*";
  112. }
  113. return temp;
  114. }
  115. /**
  116. * 拷贝临时图片到存储目录
  117. *
  118. * @param images
  119. * @return
  120. * @throws IOException
  121. */
  122. // public static String copyTempImage(String images) throws IOException {
  123. // // 文件保存的临时路径
  124. // String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  125. // // 图片保存路径
  126. // String imagePath = SystemConf.getInstance().getImagePath() + File.separator;
  127. // String[] tempImages = images.split(",");
  128. // String temp = "";
  129. // for (String image : tempImages) {
  130. // File file = new File(tempPath + image);
  131. // File smallFile = new File(tempPath + image + "_small");
  132. // if (file.exists() && smallFile.exists()) {
  133. // // 原图拷贝路径
  134. // File targetFile = new File(imagePath + image);
  135. // // 缩略图拷贝路径
  136. // File targetSmallFile = new File(imagePath + image + "_small");
  137. // // 拷贝原图
  138. // FileUtils.copyFile(file, targetFile);
  139. // // 拷贝缩略图
  140. // FileUtils.copyFile(smallFile, targetSmallFile);
  141. // // 删除临时文件
  142. // FileUtils.forceDelete(file);
  143. // FileUtils.forceDelete(smallFile);
  144. // if (temp.length() == 0) {
  145. // temp = SystemConf.getInstance().getImageServer() + image;
  146. // } else {
  147. // temp += "," + SystemConf.getInstance().getImageServer() + image;
  148. // }
  149. // }
  150. // }
  151. // return temp;
  152. // }
  153. /**
  154. * 拷贝临时语音文件到存储目录
  155. *
  156. * @param voices
  157. * @return
  158. * @throws IOException
  159. */
  160. public static String copyTempVoice(String voices) throws Exception {
  161. // 文件保存的临时路径
  162. String serverUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
  163. FastDFSUtil fastDFSUtil = new FastDFSUtil();
  164. String fileUrls = "";
  165. File f = new File(voices);
  166. if (f.exists()) {
  167. String fileName = f.getName();
  168. InputStream in = new FileInputStream(f);
  169. ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
  170. in.close();
  171. if (result != null) {
  172. fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
  173. + result.get("groupName").toString().replaceAll("\"", "") + "/"
  174. + result.get("remoteFileName").toString().replaceAll("\"", "");
  175. f.delete();
  176. }
  177. }
  178. return fileUrls;
  179. }
  180. /**
  181. * 校验健康指标是否正常
  182. *
  183. * @param curValue 当前值
  184. * @param standardMax 最大标准值
  185. * @param standardMin 最小标准值
  186. * @return 0正常,1高,-1低
  187. */
  188. public static double checkHealthIndex(double curValue, double standardMax, double standardMin) {
  189. if (curValue <= 0) {
  190. return 0;
  191. }
  192. if (standardMax > 0 && curValue > standardMax) {
  193. return curValue;
  194. }
  195. // 低于标准值,暂不推
  196. // if (standardMin > 0 && curValue < standardMin) {
  197. // return -curValue;
  198. // }
  199. return 0;
  200. }
  201. /**
  202. * 上传文件到FastDFS
  203. *
  204. * @param files 临时文件
  205. * @return
  206. * @throws Exception
  207. */
  208. public static String copyTempImage(String files) throws Exception {
  209. // 文件保存的临时路径
  210. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  211. String serverUrl = SystemConf.getInstance().getSystemProperties().getProperty("fastdfs_file_url");
  212. String[] fileArray = files.split(",");
  213. FastDFSUtil fastDFSUtil = new FastDFSUtil();
  214. String fileUrls = "";
  215. for (String file : fileArray) {
  216. File f = new File(tempPath + file);
  217. File fs = new File(tempPath + file + "_small");
  218. if (f.exists()) {
  219. String fileName = f.getName();
  220. InputStream in = new FileInputStream(f);
  221. ObjectNode result = fastDFSUtil.upload(in, fileName.substring(fileName.lastIndexOf(".") + 1), "");
  222. in.close();
  223. if (result != null) {
  224. fileUrls += (StringUtils.isEmpty(fileUrls) ? "" : ",") + serverUrl
  225. + result.get("groupName").toString().replaceAll("\"", "") + "/"
  226. + result.get("remoteFileName").toString().replaceAll("\"", "");
  227. f.delete();
  228. if (fs.exists()) {
  229. fs.delete();
  230. }
  231. }
  232. }
  233. }
  234. return fileUrls;
  235. }
  236. public static void changeToMp3(String sourcePath, String targetPath) {
  237. File source = new File(sourcePath);
  238. File target = new File(targetPath);
  239. AudioAttributes audio = new AudioAttributes();
  240. Encoder encoder = new Encoder();
  241. audio.setCodec("libmp3lame");
  242. EncodingAttributes attrs = new EncodingAttributes();
  243. attrs.setFormat("mp3");
  244. attrs.setAudioAttributes(audio);
  245. try {
  246. encoder.encode(source, target, attrs);
  247. } catch (IllegalArgumentException e) {
  248. e.printStackTrace();
  249. } catch (InputFormatException e) {
  250. e.printStackTrace();
  251. } catch (EncoderException e) {
  252. e.printStackTrace();
  253. }
  254. }
  255. public static String getVideoTimeAndImg(String sourcePath,String targetPath) throws Exception{
  256. File source = new File(sourcePath);
  257. Encoder encoder = new Encoder();
  258. MultimediaInfo m = encoder.getInfo(source);
  259. File target = new File(targetPath);//转图片
  260. VideoAttributes video = new VideoAttributes();
  261. video.setCodec("png");//转图片
  262. video.setSize(new VideoSize(600, 500));
  263. EncodingAttributes attrs = new EncodingAttributes();
  264. attrs.setFormat("image2");//转图片
  265. attrs.setOffset(0.01f);//设置偏移位置,即开始转码位置(3秒)
  266. attrs.setDuration(0.01f);//设置转码持续时间(1秒)
  267. attrs.setVideoAttributes(video);
  268. encoder.encode(source, target, attrs);
  269. return m.getDuration()+"";
  270. }
  271. public static void changeToMp4(String sourcePath, String targetPath) {
  272. File source = new File(sourcePath);
  273. File target = new File(targetPath);
  274. AudioAttributes audio = new AudioAttributes();
  275. //audio.setCodec("aac");
  276. audio.setCodec("libvorbis");
  277. // audio.setBitRate(new Integer(64000));
  278. //audio.setChannels(new Integer(1));
  279. // audio.setSamplingRate(new Integer(22050));
  280. VideoAttributes video = new VideoAttributes();
  281. //video.setCodec("libxvid");// 转MP4
  282. video.setCodec("libtheora");//
  283. // video.setBitRate(new Integer(240000));// 180kb/s比特率
  284. // video.setFrameRate(new Integer(28));// 1f/s帧频,1是目前测试比较清楚的,越大越模糊
  285. EncodingAttributes attrs = new EncodingAttributes();
  286. attrs.setFormat("ogg");// 转MP4
  287. attrs.setAudioAttributes(audio);
  288. attrs.setVideoAttributes(video);
  289. Encoder encoder = new Encoder();
  290. long beginTime = System.currentTimeMillis();
  291. try {
  292. // 获取时长
  293. MultimediaInfo m = encoder.getInfo(source);
  294. System.out.println(m.getDuration());
  295. System.out.println("获取时长花费时间是:" + (System.currentTimeMillis() - beginTime));
  296. beginTime = System.currentTimeMillis();
  297. encoder.encode(source, target, attrs);
  298. System.out.println("视频转码花费时间是:" + (System.currentTimeMillis() - beginTime));
  299. } catch (IllegalArgumentException e) {
  300. e.printStackTrace();
  301. } catch (InputFormatException e) {
  302. e.printStackTrace();
  303. } catch (EncoderException e) {
  304. e.printStackTrace();
  305. }
  306. }
  307. public static void main(String args[]) throws Exception{
  308. System.out.print("中文".length());
  309. System.out.print("中文".substring(0,30));
  310. }
  311. public static String getSubString(String content,int min,int max){
  312. if(StringUtils.isBlank(content)){
  313. return "";
  314. }else if(content.length()<=max){
  315. return content;
  316. }else{
  317. return content.substring(min,max);
  318. }
  319. }
  320. public static String saveVoiceToDisk(InputStream inputStream,String newFileName) throws Exception {
  321. // 文件保存的临时路径
  322. String tempPath = SystemConf.getInstance().getTempPath() + File.separator;
  323. // 拼接年月日路径
  324. String datePath = DateUtil.getStringDate("yyyy") + File.separator + DateUtil.getStringDate("MM") + File.separator + DateUtil.getStringDate("dd") + File.separator;
  325. // 保存路径
  326. File uploadFile = new File(tempPath + datePath + newFileName);
  327. FileOutputStream fileOutputStream = null;
  328. try {
  329. if (!uploadFile.getParentFile().exists()) {
  330. uploadFile.getParentFile().mkdirs();
  331. }
  332. byte[] data = new byte[1024];
  333. int len = 0;
  334. fileOutputStream = new FileOutputStream(uploadFile);
  335. while ((len = inputStream.read(data)) != -1) {
  336. fileOutputStream.write(data, 0, len);
  337. }
  338. // 返回保存路径
  339. return tempPath+datePath + newFileName;
  340. } catch (IOException e) {
  341. e.printStackTrace();
  342. } finally {
  343. if (inputStream != null) {
  344. try {
  345. inputStream.close();
  346. } catch (IOException e) {
  347. e.printStackTrace();
  348. }
  349. }
  350. if (fileOutputStream != null) {
  351. try {
  352. fileOutputStream.close();
  353. } catch (IOException e) {
  354. e.printStackTrace();
  355. }
  356. }
  357. }
  358. return null;
  359. }
  360. }