SysUtil.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package me.zhengjie.appapi.util;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import sun.misc.BASE64Encoder;
  5. import java.io.*;
  6. import java.text.SimpleDateFormat;
  7. import java.util.*;
  8. public class SysUtil {
  9. private static final Logger log = LoggerFactory.getLogger(SysUtil.class);
  10. /**
  11. * 生成8位随机字符串
  12. *
  13. * @return
  14. */
  15. public static String getRandomStr() {
  16. String[] chars = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
  17. "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
  18. StringBuffer shortBuffer = new StringBuffer();
  19. String uuid = UUID.randomUUID().toString().replace("-", "");
  20. for (int j = 0; j < 8; j++) {
  21. String str = uuid.substring(j * 4, j * 4 + 4);
  22. int par = Integer.parseInt(str, 16);
  23. shortBuffer.append(chars[par % 36]);
  24. }
  25. return shortBuffer.toString();
  26. }
  27. /**
  28. * 图片转base64字符串
  29. *
  30. * @param imgFile 图片路径
  31. * @return base64字符串格式的图片
  32. */
  33. public static String imageToBase64Str(String imgFile) {
  34. InputStream inputStream = null;
  35. byte[] data = null;
  36. try {
  37. inputStream = new FileInputStream(imgFile);
  38. data = new byte[inputStream.available()]; // 根据文件流字节大小初始化data字节数组大小
  39. inputStream.read(data); // 将流读入data
  40. inputStream.close(); // 关闭流
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. // 将图片数组加密
  45. BASE64Encoder encoder = new BASE64Encoder();
  46. return encoder.encode(data);
  47. }
  48. /**
  49. * 生成16位唯一性的订单号
  50. *
  51. * @return
  52. */
  53. public static String getUUID() {
  54. // 随机生成一位整数
  55. int random = (int) (Math.random() * 9 + 1);
  56. String valueOf = String.valueOf(random);
  57. // 生成uuid的hashCode值
  58. int hashCode = UUID.randomUUID().toString().hashCode();
  59. // 可能为负数
  60. if (hashCode < 0) {
  61. hashCode = -hashCode;
  62. }
  63. String value = valueOf + String.format("%015d", hashCode);
  64. return value;
  65. }
  66. /**
  67. * 视频文件转base64
  68. *
  69. * @param path 视频文件路径带文件名
  70. * @return base64
  71. */
  72. public static String videoToBase64(String path) {
  73. File videofilePath = new File(path);
  74. long size = videofilePath.length();
  75. byte[] imageByte = new byte[(int) size];
  76. FileInputStream fs = null;
  77. BufferedInputStream bis = null;
  78. try {
  79. fs = new FileInputStream(videofilePath);
  80. bis = new BufferedInputStream(fs);
  81. bis.read(imageByte);
  82. } catch (FileNotFoundException e) {
  83. log.info("文件" + videofilePath.getName() + "不能被找到:{}", e.getMessage());
  84. } catch (IOException e) {
  85. log.info("byte转换BASE64出错:" + e.getMessage());
  86. } finally {
  87. if (bis != null) {
  88. try {
  89. bis.close();
  90. } catch (IOException e) {
  91. log.info("关闭输入流出错:" + e.getMessage());
  92. }
  93. }
  94. if (fs != null) {
  95. try {
  96. fs.close();
  97. } catch (IOException e) {
  98. log.info("关闭输入流出错:" + e.getMessage());
  99. }
  100. }
  101. }
  102. return (new BASE64Encoder()).encode(imageByte);
  103. }
  104. /**
  105. * 字符串写入文件
  106. *
  107. * @param str
  108. * @param path
  109. */
  110. public static void writeToFile(String str, String path) {
  111. FileWriter writer;
  112. try {
  113. writer = new FileWriter(path);
  114. writer.write(str);
  115. writer.flush();
  116. writer.close();
  117. } catch (IOException e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. /**
  122. * 获取十分钟后的时间戳
  123. *
  124. * @return
  125. */
  126. public static long tenMinAfter() {
  127. Date date = new Date();
  128. System.out.println(date.getTime());
  129. date.setTime(date.getTime() + 10 * 60 * 1000);
  130. long time = date.getTime();
  131. return time;
  132. }
  133. /**
  134. * 将文件转换成byte数组
  135. *
  136. * @param tradeFile
  137. * @return
  138. */
  139. public static byte[] File2byte(File tradeFile) {
  140. byte[] buffer = null;
  141. try {
  142. FileInputStream fis = new FileInputStream(tradeFile);
  143. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  144. byte[] b = new byte[1024];
  145. int n;
  146. while ((n = fis.read(b)) != -1) {
  147. bos.write(b, 0, n);
  148. }
  149. fis.close();
  150. bos.close();
  151. buffer = bos.toByteArray();
  152. } catch (FileNotFoundException e) {
  153. e.printStackTrace();
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. }
  157. return buffer;
  158. }
  159. /**
  160. * 通过文件名获取证件号
  161. *
  162. * @param fileName
  163. * @return
  164. */
  165. public static String fileNameToIdNo(String fileName) {
  166. int i = fileName.lastIndexOf("-");
  167. String substring = fileName.substring(i + 1, i + 18 + 1);
  168. return substring;
  169. }
  170. /**
  171. * 将本地文件转Base64
  172. *
  173. * 转Base64
  174. *
  175. * @param videofilePath
  176. * @return
  177. */
  178. public static String fileToBase64(File videofilePath) {
  179. long size = videofilePath.length();
  180. byte[] imageByte = new byte[(int) size];
  181. FileInputStream fs = null;
  182. BufferedInputStream bis = null;
  183. try {
  184. fs = new FileInputStream(videofilePath);
  185. bis = new BufferedInputStream(fs);
  186. bis.read(imageByte);
  187. } catch (FileNotFoundException e) {
  188. e.printStackTrace();
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. } finally {
  192. if (bis != null) {
  193. try {
  194. bis.close();
  195. } catch (IOException e) {
  196. e.printStackTrace();
  197. }
  198. }
  199. if (fs != null) {
  200. try {
  201. fs.close();
  202. } catch (IOException e) {
  203. e.printStackTrace();
  204. }
  205. }
  206. }
  207. return Base64.getEncoder().encodeToString(imageByte);
  208. }
  209. /**
  210. * 证件号解析性别年龄
  211. *
  212. * @param CardCode
  213. * @return
  214. * @throws Exception
  215. */
  216. public static Map<String, String> getCarInfo(String CardCode) {
  217. Map<String, String> map = new HashMap<String, String>();
  218. try {
  219. String year = CardCode.substring(6).substring(0, 4);// 得到年份
  220. String yue = CardCode.substring(10).substring(0, 2);// 得到月份
  221. // String day=CardCode.substring(12).substring(0,2);//得到日
  222. String sex;
  223. if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
  224. sex = "女";
  225. } else {
  226. sex = "男";
  227. }
  228. Date date = new Date();// 得到当前的系统时间
  229. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  230. String fyear = format.format(date).substring(0, 4);// 当前年份
  231. String fyue = format.format(date).substring(5, 7);// 月份
  232. // String fday=format.format(date).substring(8,10);
  233. int age = 0;
  234. if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
  235. age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
  236. } else {// 当前用户还没过生
  237. age = Integer.parseInt(fyear) - Integer.parseInt(year);
  238. }
  239. map.put("sex", sex);
  240. map.put("age", String.valueOf(age));
  241. } catch (Exception e) {
  242. map.put("sex", "男");
  243. map.put("age", "22");
  244. }
  245. return map;
  246. }
  247. /**
  248. * 判断操作系统是否为Windows
  249. * @return
  250. */
  251. public static boolean isWindows() {
  252. String osName = System.getProperty("os.name");
  253. if (osName.toLowerCase().contains("windows")) {
  254. return true;
  255. }
  256. return false;
  257. }
  258. public static void main(String[] args) {
  259. isWindows();
  260. }
  261. }