CompressEncodeingUtil.java 2.75 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
package com.lyms.hospital.util;

import com.lyms.util.DateUtil;

/**
* 水印生成代码
*
*/
public class CompressEncodeingUtil
{

final static char[] digits = {
'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' ,
'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' , '$' , '!' ,
};

/**
* 把10进制的数字转换成64进制
* @param number
* @return
*/
public static String compressNumber(String number) {
return compressNumber(Long.valueOf(number));
}

/**
* 把10进制的数字转换成64进制
* @param number
* @return
*/
public static String compressNumber(long number) {
System.out.println(number);
char[] buf = new char[64];
int charPos = 64;
int radix = 1 << 6;
long mask = radix - 1;
do {
buf[--charPos] = digits[(int)(number & mask)];
number >>>= 6;
} while (number != 0);
return new String(buf, charPos, (64 - charPos));
}
/**
* 把64进制的字符串转换成10进制
* @param decompStr
* @return
*/
public static long unCompressNumber(String decompStr)
{
long result=0;
for (int i = decompStr.length()-1; i >=0; i--) {
if(i==decompStr.length())
{
result+=getCharIndexNum(decompStr.charAt(i));
continue;
}
for (int j = 0; j < digits.length; j++) {
if(decompStr.charAt(i)==digits[j])
{
result+=((long)j)<<6*(decompStr.length()-1-i);
}
}
}
return result;
}
/**
*
* @param ch
* @return
*/
private static long getCharIndexNum(char ch)
{
int num=((int)ch);
if(num>=48&&num<=57)
{
return num-48;
}
else if(num>=97&&num<=122)
{
return num-87;
}else if(num>=65&&num<=90)
{
return num-29;
}else if(num==43)
{
return 62;
}
else if (num == 47)
{
return 63;
}
return 0;
}

/**
* 获取水印
* @param userId
* @return
*/
public static String getWaterMark(String userId){
return CompressEncodeingUtil.compressNumber(userId + DateUtil.getymd6());
}
}