如何动态生成唯一(短)URL 文件夹名称...例如 Bit.ly

发布于 2024-10-31 13:00:18 字数 314 浏览 0 评论 0原文

我正在创建一个应用程序,它将在 Web 服务器上创建大量文件夹,其中包含文件。

我需要文件夹名称是唯一的。我可以使用 GUID 轻松完成此操作,但我想要更用户友好的东西。它不需要由用户说出,但应该是简短的标准字符(字母最好)。

简而言之:我希望利用其独特的名称做一些像 Bit.ly 那样的事情:

www.mydomain.com/ABCDEF

有关于如何执行此操作的良好参考吗?我的平台将是 .NET/C#,但可以提供有关一般概念的任何帮助、参考资料、链接等,或者解决此任务的任何总体建议。

I'm creating an application which will create a large number of folders on a web server, with files inside of them.

I need the folder name to be unique. I can easily do this with a GUID, but I want something more user friendly. It doesn't need to be speakable by users, but should be short and standard characters (alphas is best).

In short: i'm looking to do something like Bit.ly does with their unique names:

www.mydomain.com/ABCDEF

Is there a good reference on how to do this? My platform will be .NET/C#, but ok with any help, references, links, etc on the general concept, or any overall advice to solve this task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冰火雁神 2024-11-07 13:00:18

从 1 开始。递增到 2, 3, 4, 5, 6, 7,

8, 9, a, b...

A, B, C...

X, Y, Z, 10, 11, 12, .. 1a、1b,

你明白了。

您有一个同步的全局 int/long“下一个 id”,并以 62 基数(数字、小写字母、大写字母)或 36 基数或其他形式表示它。

Start at 1. Increment to 2, 3, 4, 5, 6, 7,

8, 9, a, b...

A, B, C...

X, Y, Z, 10, 11, 12, ... 1a, 1b,

You get the idea.

You have a synchronized global int/long "next id" and represent it in base 62 (numbers, lowercase, caps) or base 36 or something.

靖瑶 2024-11-07 13:00:18

我假设您知道如何使用 Web 服务器的重定向功能。如果您需要帮助,请发表评论:)。

我的方法是生成一个随机整数(在“a”和“z”的整数值之间);将其转换为字符;将其附加到字符串;并重复直到达到所需的长度。如果它生成的值已存在于数据库中,请重复该过程。如果它是唯一的,则将其与实际位置的名称和别名一起存储在数据库中。

这有点像 hack,因为它假设“a”到“z”的整数值实际上是按顺序排列的。

我能想到的最好的:(。

I'm assuming that you know how to use your web server's redirect capabilities. If you need help, just comment :).

The way I would do it would be generating a random integer (between the integer values of 'a' and 'z'); converting it into a char; appending it to a string; and repeating until we reach the needed length. If it generates a value already in the database, repeat the process. If it was unique, store it in the database with the name of the actual location and the name of the alias.

This is a bit hack-like because it assumes that 'a' through 'z' are actually in sequence in their integer values.

Best I could think of :(.

违心° 2024-11-07 13:00:18

在 Perl 中,没有模块,因此您可以更轻松地翻译。

sub convert_to_base {
    my ($n, $b) = @_;
    my @digits;
    while ($n) {
        my $digits = $n % $b;
        unshift @digits, $digit;
        $n = ($n - $digit) / $b;
    }
    unshift @digits, 0 if !@digits;
    return @digits;
}

# Whatever characters you want to use.
my @digit_set = ( '0'..'9', 'a'..'z', 'A'..'Z' );

# The id of the record in the database,
# or one more than the last id you generated.
my $id = 1;

my $converted =
    join '',
    map { $digit_set[$_] }
    convert_to_base($id, 0+@digits_set);

In Perl, without modules so you can translate more easly.

sub convert_to_base {
    my ($n, $b) = @_;
    my @digits;
    while ($n) {
        my $digits = $n % $b;
        unshift @digits, $digit;
        $n = ($n - $digit) / $b;
    }
    unshift @digits, 0 if !@digits;
    return @digits;
}

# Whatever characters you want to use.
my @digit_set = ( '0'..'9', 'a'..'z', 'A'..'Z' );

# The id of the record in the database,
# or one more than the last id you generated.
my $id = 1;

my $converted =
    join '',
    map { $digit_set[$_] }
    convert_to_base($id, 0+@digits_set);
苯莒 2024-11-07 13:00:18

我需要类似于你想要完成的事情。我重新设计了代码来生成文件夹,所以试试这个。它是为控制台应用程序设置的,但您也可以在网站中使用它。

    private static void genRandomFolders()
    {
        string basepath = "C:\\Users\\{username here}\\Desktop\\";
        int count = 5;
        int length = 8;

        List<string> codes = new List<string>();
        int total = 0;
        int i = count;
        Random rnd = new Random();
        while (i-- > 0)
        {
            string code = RandomString(rnd, length);
            if (!codes.Exists(delegate(string c) { return c.ToLower() == code.ToLower(); }))
            {
                //Create directory here
                System.IO.Directory.CreateDirectory(basepath + code);
            }
            total++;
            if (total % 100 == 0)
                Console.WriteLine("Generated " + total.ToString() + " random folders...");
        }

        Console.WriteLine();
        Console.WriteLine("Generated " + total.ToString() + " total random folders.");
    }
    public static string RandomString(Random r, int len)
    {
        //string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //uppercase only
        //string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; //All
        string str = "abcdefghjkmnpqrstuvwxyz123456789"; //Lowercase only
        StringBuilder sb = new StringBuilder(); 
        while ((len--) > 0)
            sb.Append(str[(int)(r.NextDouble() * str.Length)]);
        return sb.ToString(); 
    }

I needed something similar to what you're trying to accomplish. I retooled my code to generate folders so try this. It's setup for a console app, but you can use it in a website also.

    private static void genRandomFolders()
    {
        string basepath = "C:\\Users\\{username here}\\Desktop\\";
        int count = 5;
        int length = 8;

        List<string> codes = new List<string>();
        int total = 0;
        int i = count;
        Random rnd = new Random();
        while (i-- > 0)
        {
            string code = RandomString(rnd, length);
            if (!codes.Exists(delegate(string c) { return c.ToLower() == code.ToLower(); }))
            {
                //Create directory here
                System.IO.Directory.CreateDirectory(basepath + code);
            }
            total++;
            if (total % 100 == 0)
                Console.WriteLine("Generated " + total.ToString() + " random folders...");
        }

        Console.WriteLine();
        Console.WriteLine("Generated " + total.ToString() + " total random folders.");
    }
    public static string RandomString(Random r, int len)
    {
        //string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //uppercase only
        //string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; //All
        string str = "abcdefghjkmnpqrstuvwxyz123456789"; //Lowercase only
        StringBuilder sb = new StringBuilder(); 
        while ((len--) > 0)
            sb.Append(str[(int)(r.NextDouble() * str.Length)]);
        return sb.ToString(); 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文