CommonUtil.java 13 KB

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