java achieve URL short connection

Lite JAVA generate a fixed four short connection

Introduction: using base64 and md5, generate a short fixed length of four bits is connected (not including the domain)

For example: http: // Nolog / asdasdasd / asdasda / aqwertyuiopasdfghjklzxcvbnm5132456789

effect:

Code:

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

java.util.Base64 import;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**

* Achieve short URL, no matter how long, will generate four links

*

* @author CHX

*

*/

public class ShortURL {

private static String plainUrl = "http://Nolog/asdasdasd/asdasda/aqwertyuiopasdfghjklzxcvbnm5132456789";

private static String[] chars = new String[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" };

public static void main(String[] args) {
  myTestShort(plainUrl, "Nolog");
}

/ **
* First get contents of a fixed format from a URL
*
* @param longUrl
* original url
* @param YUMING
* domain name
* /
public static void myTestShort (longUrl String, String YUMING) {

String regex = "(http://|https://)" + yuMing + "(.*)";

Pattern r = Pattern.compile(regex);

// Now create an object matcher

Matcher m = r.matcher (longUrl);

if (m.find()) {

String url = m.group(2);

if (url != null) {

// here is generated by four short connection

System.out.println(m.group(1) + yuMing + "/" + changes(url));

}

}

}

/ **
* coding ideas: Taking into account the base64 encoding, url only [0-9] [az] [AZ ] these types of characters, all the characters in a total of 26 + 26 + 10 = 62 kinds of the corresponding mapping table 62 into can be prepared
*
* @param value
* @return
* /
public static String Changes (String value) {

// Get base64 encoding

String stringBase64 = stringBase64(value);

// remove the last == (This feature is base64, finally ending ==)

stringBase64 = stringBase64.substring(0, stringBase64.length() - 2);

MessageDigest md5 = null;

try {

  md5 = MessageDigest.getInstance("MD5");

} catch (NoSuchAlgorithmException e) {

  e.printStackTrace ();

}

// md5 generated using 32-bit string of fixed length

String mid = new String(bytesToHexString(md5.digest(stringBase64.getBytes())));

StringBuilder outChars = new StringBuilder();

for (int i = 0; i < 4; i++) {

// set every eight a

String sTempSubString = mid.substring(i * 8, i * 8 + 8);

// find ways to reduce the number of characters in this eight hexadecimal to less than 62, so take the remainder, then replaced the corresponding alphanumeric

outChars.append(chars[(int) (Long.parseLong(sTempSubString, 16) % chars.length)]);

  }

  return outChars.toString();
}

/ **
* Converts a string to a base64 encoded
*
* @param text
* Original
* @return
* /
public static stringBase64 String (String text) {
  return Base64.getEncoder () encodeToString (text.getBytes ());.
}

/**
* 将byte转换为16进制的字符串
*
* @param src
* @return
*/
public static String bytesToHexString(byte[] src) {
  StringBuilder stringBuilder = new StringBuilder();
  if (src == null || src.length <= 0) {
    return null;
  }
  for (int i = 0; i < src.length; i++) {
    int v = src[i] & 0xFF;
    String hv = Integer.toHexString(v);
    if (hv.length() < 2) {
      stringBuilder.append(0);
    }
    stringBuilder.append(hv);
  }
  return stringBuilder.toString();
}

}

Guess you like

Origin www.cnblogs.com/chxwkx/p/11266295.html