-2

In my Java spring-based project, there is a web url that I want to send to users via message.

But I'm looking for a solution to not give a security issue and send it both secured and short instead of forwarding a long link to customers.

Waiting for your solutions. Thanks.

AnilF
  • 108
  • 7
  • Does this answer your question? [How do I create a URL shortener?](https://stackoverflow.com/questions/742013/how-do-i-create-a-url-shortener) – pringi Jul 08 '22 at 08:13
  • nope, i have tried that in a compiler. Let me add the code as answer below. Can you check? – AnilF Jul 08 '22 at 10:25

1 Answers1

0
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        UrlShortener url = new UrlShortener();
        System.out.println(url.decode("https://www.google.com"));
    }
}

public class UrlShortener {
    private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final int    BASE     = ALPHABET.length();

    public static String encode(int num) {
        StringBuilder sb = new StringBuilder();
        while ( num > 0 ) {
            sb.append( ALPHABET.charAt( num % BASE ) );
            num /= BASE;
        }
        return sb.reverse().toString();   
    }

    public static int decode(String str) {
        int num = 0;
        for ( int i = 0; i < str.length(); i++ )
            num = num * BASE + ALPHABET.indexOf(str.charAt(i));
        return num;
    }   
}
Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52
AnilF
  • 108
  • 7