生成url格式base64编码的uuid

  1. import java.nio.charset.StandardCharsets;
  2. import java.util.Base64;
  3. import java.util.UUID;
  4. public class Base64UUIDHelper {
  5. public Base64UUIDHelper() {
  6. }
  7. public static String getBase64Uuid() {
  8. UUID temp = UUID.randomUUID();
  9. byte[] bytes = new byte[16];
  10. long half = temp.getLeastSignificantBits();
  11. int i;
  12. for(i = 0; i < 8; ++i) {
  13. bytes[i] = (byte)((int)(half & 255L));
  14. half >>= 8;
  15. }
  16. half = temp.getMostSignificantBits();
  17. for(i = 8; i < 16; ++i) {
  18. bytes[i] = (byte)((int)(half & 255L));
  19. half >>= 8;
  20. }
  21. return (new String(Base64.getUrlEncoder().encode(bytes), StandardCharsets.UTF_8)).substring(0, 22);
  22. }
  23. }