在 Java 中创建 GUID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2982748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:13:50  来源:igfitidea点击:

Create a GUID in Java

javaguid

提问by Chris Dutrow

What are some of the best ways to create a GUID in Java?

在 Java 中创建 GUID 的最佳方法有哪些?

采纳答案by Mark Byers

Have a look at the UUID classbundled with Java 5 and later.

查看与 Java 5 及更高版本捆绑在一起的UUID 类

For example:

例如:

回答by Kaleb Brasee

java.util.UUID.randomUUID();

java.util.UUID.randomUUID();

回答by Stephen C

It depends what kind of UUIDyou want.

这取决于你想要什么样的UUID

  • The standard Java UUIDclass generates Version 4(random) UUIDs. (UPDATE- Version 3(name) UUIDs can also be generated.) It can also handle other variants, though it cannot generate them. (In this case, "handle" means construct UUIDinstances from long, byte[]or Stringrepresentations, and provide some appropriate accessors.)

  • The Java UUID Generator (JUG)implementation purports to support "all 3 'official' types of UUID as defined by RFC-4122" ... though the RFC actually defines 4 types and mentions a 5th type.

  • 标准 JavaUUID类生成版本 4(随机)UUID。(更新-也可以生成版本 3(名称)UUID。)它也可以处理其他变体,尽管它不能生成它们。(在这种情况下,“句柄”意味着UUIDlongbyte[]String表示构造实例,并提供一些适当的访问器。)

  • Java的UUID生成器(士)“所界定的所有3‘官方’类型UUID的实施声称支持RFC-4122” ......尽管RFC实际上定义了4种类型,并提到第5类型。

For more information on UUID types and variants, there is a good summary in Wikipedia, and the gory details are in RFC 4122and the other specifications.

有关 UUID 类型和变体的更多信息,维基百科中有一个很好的总结,而血腥的细节在RFC 4122和其他规范中。

回答by Basil Bourque

The other Answers are correct, especially this oneby Stephen C.

其他的答案是正确的,尤其是这一个斯蒂芬·Ç

Reaching Outside Java

到达 Java 之外

Generating a UUIDvalue within Java is limited to Version 4 (random)because of security concerns.

出于安全考虑,在 Java 中生成 UUID值仅限于版本 4(随机)

If you want other versions of UUIDs, one avenue is to have your Java app reach outside the JVMto generate UUIDs by calling on:

如果您想要其他版本的 UUID,一种方法是让您的 Java 应用程序到达JVM之外以通过调用以下方法生成 UUID:

  • Command-line utility
    Bundled with nearly every operating system.
    For example, uuidgenfound in Mac OS X, BSD, and Linux.
  • Database server
    Use JDBCto retrieve a UUID generated on the database server.
    For example, the uuid-osspextension often bundled with Postgres. That extension can generates Versions 1, 3, and 4 values and additionally a couple variations:
    • uuid_generate_v1mc()–?generates a version 1 UUID but uses a random multicast MAC address instead of the real MAC address of the computer.
    • uuid_generate_v5(namespace uuid, name text)–?generates a version 5 UUID, which works like a version 3 UUID except that SHA-1 is used as a hashing method.
  • Web Service
    For example, UUID Generatorcreates Versions 1 & 3 as well as nil valuesand GUID.
  • 命令行实用程序
    几乎与每个操作系统捆绑在一起。
    例如,uuidgen可在 Mac OS X、BSD 和 Linux 中找到。
  • 数据库服务器
    使用JDBC检索在数据库服务器上生成的 UUID。
    例如,uuid-ossp扩展通常与Postgres捆绑在一起。该扩展可以生成版本 1、3 和 4 值以及另外几个变体:
    • uuid_generate_v1mc()–? 生成版本 1 UUID,但使用随机多播 MAC 地址而不是计算机的真实 MAC 地址。
    • uuid_generate_v5(namespace uuid, name text)–? 生成第 5 版 UUID,其工作方式与第 3 版 UUID 类似,只是使用 SHA-1 作为散列方法。
  • Web 服务
    例如,UUID 生成器创建版本 1 和 3 以及nil 值GUID

回答by Anton Belev

Just to extend Mark Byers's answer with an example:

只是用一个例子来扩展 Mark Byers 的回答:

import java.util.UUID;

public class RandomStringUUID {
    public static void main(String[] args) {
        UUID uuid = UUID.randomUUID();
        System.out.println("UUID=" + uuid.toString() );
    }
}