| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- package me.zhengjie.appapi.util;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import sun.misc.BASE64Encoder;
- import java.io.*;
- import java.text.SimpleDateFormat;
- import java.util.*;
- public class SysUtil {
- private static final Logger log = LoggerFactory.getLogger(SysUtil.class);
- /**
- * 生成8位随机字符串
- *
- * @return
- */
- public static String getRandomStr() {
- String[] chars = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F",
- "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
- StringBuffer shortBuffer = new StringBuffer();
- String uuid = UUID.randomUUID().toString().replace("-", "");
- for (int j = 0; j < 8; j++) {
- String str = uuid.substring(j * 4, j * 4 + 4);
- int par = Integer.parseInt(str, 16);
- shortBuffer.append(chars[par % 36]);
- }
- return shortBuffer.toString();
- }
- /**
- * 图片转base64字符串
- *
- * @param imgFile 图片路径
- * @return base64字符串格式的图片
- */
- public static String imageToBase64Str(String imgFile) {
- InputStream inputStream = null;
- byte[] data = null;
- try {
- inputStream = new FileInputStream(imgFile);
- data = new byte[inputStream.available()]; // 根据文件流字节大小初始化data字节数组大小
- inputStream.read(data); // 将流读入data
- inputStream.close(); // 关闭流
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 将图片数组加密
- BASE64Encoder encoder = new BASE64Encoder();
- return encoder.encode(data);
- }
- /**
- * 生成16位唯一性的订单号
- *
- * @return
- */
- public static String getUUID() {
- // 随机生成一位整数
- int random = (int) (Math.random() * 9 + 1);
- String valueOf = String.valueOf(random);
- // 生成uuid的hashCode值
- int hashCode = UUID.randomUUID().toString().hashCode();
- // 可能为负数
- if (hashCode < 0) {
- hashCode = -hashCode;
- }
- String value = valueOf + String.format("%015d", hashCode);
- return value;
- }
- /**
- * 视频文件转base64
- *
- * @param path 视频文件路径带文件名
- * @return base64
- */
- public static String videoToBase64(String path) {
- File videofilePath = new File(path);
- long size = videofilePath.length();
- byte[] imageByte = new byte[(int) size];
- FileInputStream fs = null;
- BufferedInputStream bis = null;
- try {
- fs = new FileInputStream(videofilePath);
- bis = new BufferedInputStream(fs);
- bis.read(imageByte);
- } catch (FileNotFoundException e) {
- log.info("文件" + videofilePath.getName() + "不能被找到:{}", e.getMessage());
- } catch (IOException e) {
- log.info("byte转换BASE64出错:" + e.getMessage());
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- log.info("关闭输入流出错:" + e.getMessage());
- }
- }
- if (fs != null) {
- try {
- fs.close();
- } catch (IOException e) {
- log.info("关闭输入流出错:" + e.getMessage());
- }
- }
- }
- return (new BASE64Encoder()).encode(imageByte);
- }
- /**
- * 字符串写入文件
- *
- * @param str
- * @param path
- */
- public static void writeToFile(String str, String path) {
- FileWriter writer;
- try {
- writer = new FileWriter(path);
- writer.write(str);
- writer.flush();
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 获取十分钟后的时间戳
- *
- * @return
- */
- public static long tenMinAfter() {
- Date date = new Date();
- System.out.println(date.getTime());
- date.setTime(date.getTime() + 10 * 60 * 1000);
- long time = date.getTime();
- return time;
- }
- /**
- * 将文件转换成byte数组
- *
- * @param tradeFile
- * @return
- */
- public static byte[] File2byte(File tradeFile) {
- byte[] buffer = null;
- try {
- FileInputStream fis = new FileInputStream(tradeFile);
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- byte[] b = new byte[1024];
- int n;
- while ((n = fis.read(b)) != -1) {
- bos.write(b, 0, n);
- }
- fis.close();
- bos.close();
- buffer = bos.toByteArray();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return buffer;
- }
- /**
- * 通过文件名获取证件号
- *
- * @param fileName
- * @return
- */
- public static String fileNameToIdNo(String fileName) {
- int i = fileName.lastIndexOf("-");
- String substring = fileName.substring(i + 1, i + 18 + 1);
- return substring;
- }
- /**
- * 将本地文件转Base64
- *
- * 转Base64
- *
- * @param videofilePath
- * @return
- */
- public static String fileToBase64(File videofilePath) {
- long size = videofilePath.length();
- byte[] imageByte = new byte[(int) size];
- FileInputStream fs = null;
- BufferedInputStream bis = null;
- try {
- fs = new FileInputStream(videofilePath);
- bis = new BufferedInputStream(fs);
- bis.read(imageByte);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (bis != null) {
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (fs != null) {
- try {
- fs.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return Base64.getEncoder().encodeToString(imageByte);
- }
- /**
- * 证件号解析性别年龄
- *
- * @param CardCode
- * @return
- * @throws Exception
- */
- public static Map<String, String> getCarInfo(String CardCode) {
- Map<String, String> map = new HashMap<String, String>();
- try {
- String year = CardCode.substring(6).substring(0, 4);// 得到年份
- String yue = CardCode.substring(10).substring(0, 2);// 得到月份
- // String day=CardCode.substring(12).substring(0,2);//得到日
- String sex;
- if (Integer.parseInt(CardCode.substring(16).substring(0, 1)) % 2 == 0) {// 判断性别
- sex = "女";
- } else {
- sex = "男";
- }
- Date date = new Date();// 得到当前的系统时间
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- String fyear = format.format(date).substring(0, 4);// 当前年份
- String fyue = format.format(date).substring(5, 7);// 月份
- // String fday=format.format(date).substring(8,10);
- int age = 0;
- if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { // 当前月份大于用户出身的月份表示已过生
- age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
- } else {// 当前用户还没过生
- age = Integer.parseInt(fyear) - Integer.parseInt(year);
- }
- map.put("sex", sex);
- map.put("age", String.valueOf(age));
- } catch (Exception e) {
- map.put("sex", "男");
- map.put("age", "22");
- }
- return map;
- }
- /**
- * 判断操作系统是否为Windows
- * @return
- */
- public static boolean isWindows() {
- String osName = System.getProperty("os.name");
- if (osName.toLowerCase().contains("windows")) {
- return true;
- }
- return false;
- }
- public static void main(String[] args) {
- isWindows();
- }
- }
|