Java implementation of a short address service method (with code) _java

Source: Internet
Author: User

If the following is a list of links to your video site, if people want to crawl your data is very easy, see the rules will know that the database is a sequential increase

Http://www.xxxx.com/video/1
Http://www.xxxx.com/video/2
Http://www.xxxx.com/video/3

So to solve this problem, we can use the short address, do not expose the real link, using symmetric encryption is a good solution.

Hashids is a good choice, it provides the implementation of Js/php/java/python and other programming languages, I use it here.

Below is the Java Short Address service I built based on the blade framework.

CREATE TABLE ' T_url ' (
 ' id ' int () NOT NULL auto_increment,
 ' url ' text not null,
 PRIMARY KEY (' id ')
) ENG Ine=innodb auto_increment=15 DEFAULT Charset=utf8;

Routing

@Path public class Indexroute {//Salt value private static final hashids Hashids = new Hashids ("Blade-shorturl");
 
  Private Urlmodel Urlmodel = new Urlmodel (); @Route ("/:key") public void Get (Request req, Response Response) {String key = Req.pathparam (": Key"). ReplaceAll ("[^a
    -ZA-Z0-9] "," "");
 
    long[] numbers = Hashids.decode (key);
      if (null = = Numbers | | Numbers.length < 1) {Response.text ("not Found");
    Return
    int id = (int) numbers[0];
    String result = Get (ID). GETURL ();
      if (result = = null) {Response.text ("not Found");
    Return
  } Response.Redirect (Result);
  @Route (value = "/", method = httpmethod.get) Public String index () {return ' index '; @Route (value = "/", method = httpmethod.post) public String Save (Request request, Response Response) {Strin
     
    G resjsp = "index";
     
    String Longurl = request.query ("url");
 if (!isurl (Longurl)) {Request.attribute ("error", "Invalid URL");     return resjsp;
    Integer id = this.save (longurl);
      if (id = = 0) {request.attribute ("error", "Save Failed");
    return resjsp;
    String hash = Hashids.encode (ID);
     
    Request.attribute ("Url_hash", hash);
     
    SYSTEM.OUT.PRINTLN ("id =" + id + ", url_hash=" + hash);
  return resjsp;
  Private Integer Save (String URL) {return Urlmodel.insert (). param ("url", url). Executeandcommit ();
  Private Urlmodel get (int id) {return urlmodel.fetchbypk (ID); Private final String REGEX = "\\b" (https?| Ftp|file)://[-a-za-z0-9+&@#/%?=~_|!:,.;]
   
  *[-a-za-z0-9+&@#/%=~_|] ";
      Private Boolean isurl (String URL) {if (Stringkit.isnotblank (URL)) {Pattern pattern = pattern.compile (REGEX);
      Matcher Matcher = pattern.matcher (URL);
      if (Matcher.find ()) {return true;
  return false; }
}

Implementation effect:

Code Location: Https://github.com/bladejava/blade-shorturl

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.