Fork me on GitHub

短url生成类

ShortUrl.java


/** * Project Name:report * File Name:ShortUrl.java * Package Name:com.riambsoft.report.util * Date:2013-3-12上午11:16:53 * Copyright (c) 2013, riambsoft All Rights Reserved. * */  package com.riambsoft.report.util;  import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;  import com.itextpdf.text.log.SysoLogger;  /** * ClassName:ShortUrl <br/> Function: TODO ADD FUNCTION. <br/> Reason: TODO ADD REASON. <br/> Date: 2013-3-12 上午11:16:53 <br/> *  * @author Administrator * @version * @since JDK 1.5 * @see */public class ShortUrl {      public static void main(String[] args) throws MalformedURLException {        String url = "http://localhost:8080/report/outPdf?docNo=inv4200_p&progCode=inv4200_p&criDesc=-+2013%2F03+%E5%B8%90%E7%B0%BF&sourceflag=&isLocal=false&rs10ucode=adnin&querySql=++company_code%3D%2700%27+and++warehouse_code+in++%28select+warehouse_code+from+inv_user_def++where+company_code%3D%2700%27+and+user_unique_no%3D%2700382%27%29+and+exists%28select+item_code+from+inv_master+where+inv_master.company_code+%3D+inv_book_master.company_code+and+inv_master.item_code+%3D+inv_book_master.item_code%29++order+by++warehouse_code%2C+item_code&companyCode=00&sessionCode=388CEF0500AE4006ADE8E052C227BC70";        for (String string : ShortText(url)) {            print(string);        }    }      public static String[] ShortText(String string) {        String key = "XuLiang"; // 自定义生成MD5加密字符串前的混合KEY        String[] chars = new String[] { // 要使用生成URL的字符        "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" };          String hex = Encript.md5(key + string);        int hexLen = hex.length();        int subHexLen = hexLen / 8;        String[] ShortStr = new String[4];          for (int i = 0; i < subHexLen; i++) {            String outChars = "";            int j = i + 1;            String subHex = hex.substring(i * 8, j * 8);            long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16);              for (int k = 0; k < 6; k++) {                int index = (int) (Long.valueOf("0000003D", 16) & idx);                outChars += chars[index];                idx = idx >> 5;            }            ShortStr[i] = outChars;        }          return ShortStr;    }      public static URL ExpandUrl(URL shortUrl) {        URL longUrl = null;        try {            HttpURLConnection req = (HttpURLConnection) shortUrl                    .openConnection();            req.setInstanceFollowRedirects(false);            req.connect();            if ((req.getResponseCode() == req.HTTP_MOVED_TEMP)||(req.getResponseCode()==req.HTTP_MOVED_PERM)) {                longUrl = new URL(req.getHeaderField("Location"));            }        } catch (Exception e) {            longUrl = null;        }        return longUrl;      }          private static void print(String messagr) throws MalformedURLException {        System.out.println(ExpandUrl(new URL("http://nutz.cn/1L")));         System.out.println(messagr);    }  }



Encript.java


/** * Project Name:report * File Name:ShortUrlUtil.java * Package Name:com.riambsoft.report.util * Date:2013-3-12上午11:07:16 * Copyright (c) 2013, riambsoft All Rights Reserved. * */  package com.riambsoft.report.util;  import java.security.MessageDigest;  /** * ClassName:ShortUrlUtil <br/> Function: TODO ADD FUNCTION. <br/> Reason: TODO ADD REASON. <br/> Date: 2013-3-12 上午11:07:16 <br/> *  * @author Administrator * @version * @since JDK 1.5 * @see */public class Encript {      // 十六进制下数字到字符的映射数组    private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };      /** 把inputString加密 */    public static String md5(String inputStr) {        return encodeByMD5(inputStr);    }      /**     * 验证输入的密码是否正确     *      * @param password     *            真正的密码(加密后的真密码)     * @param inputString     *            输入的字符串     * @return 验证结果,boolean类型     */    public static boolean authenticatePassword(String password, String inputString) {        if (password.equals(encodeByMD5(inputString))) {            return true;        } else {            return false;        }    }      /** 对字符串进行MD5编码 */    private static String encodeByMD5(String originString) {        if (originString != null) {            try {                // 创建具有指定算法名称的信息摘要                MessageDigest md5 = MessageDigest.getInstance("MD5");                // 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算                byte[] results = md5.digest(originString.getBytes());                // 将得到的字节数组变成字符串返回                String result = byteArrayToHexString(results);                return result;            } catch (Exception e) {                e.printStackTrace();            }        }        return null;    }      /**     * 轮换字节数组为十六进制字符串     *      * @param b     *            字节数组     * @return 十六进制字符串     */    private static String byteArrayToHexString(byte[] b) {        StringBuffer resultSb = new StringBuffer();        for (int i = 0; i < b.length; i++) {            resultSb.append(byteToHexString(b[i]));        }        return resultSb.toString();    }      // 将一个字节转化成十六进制形式的字符串    private static String byteToHexString(byte b) {        int n = b;        if (n < 0)            n = 256 + n;        int d1 = n / 16;        int d2 = n % 16;        return hexDigits[d1] + hexDigits[d2];    }      /**     * main:(这里用一句话描述这个方法的作用). <br/>     *      * @param args     * @since JDK 1.5     */    public static void main(String[] args) {          // TODO Auto-generated method stub      }  }


短url还原工具类


public static URL ExpandUrl(URL shortUrl) {        URL longUrl = null;        try {            HttpURLConnection req = (HttpURLConnection) shortUrl                    .openConnection();            req.setInstanceFollowRedirects(false);            req.connect();            if ((req.getResponseCode() == req.HTTP_MOVED_TEMP)||(req.getResponseCode()==req.HTTP_MOVED_PERM)) {                longUrl = new URL(req.getHeaderField("Location"));            }        } catch (Exception e) {            longUrl = null;        }        return longUrl;       }


posted @ 2013-03-18 09:05  _落雨  阅读(263)  评论(0编辑  收藏  举报