短网址生成相关


/***
 * 短链接转换工具类
 *
 * @author Administrator
 *
 */
public class ShortUrlHelper {

 

    public static CloseableHttpClient httpClient;


    static {
        httpClient = HttpClients.createDefault();
    }


    /**
     * 将长链接转为短链接(调用的新浪的短网址API),需接入相应API
     *
     * @param url
     *            需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static String convertSinaShortUrl(String url) {
        try {
            // 调用新浪API
            HttpPost post = new HttpPost("http://api.t.sina.com.cn/short_url/shorten.json");
            List<NameValuePair> params = new LinkedList<NameValuePair>();
            // 必要的url长链接参数
            params.add(new BasicNameValuePair("url_long", url));
            // 必要的新浪key
            params.add(new BasicNameValuePair("source", "3271760578"));
            post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(post);
            // 得到调用新浪API后成功返回的json字符串
            // url_short : 短链接地址 type:类型 url_long:原始长链接地址
            String json = EntityUtils.toString(response.getEntity(), "utf-8");
            List<Map<String,Object>> resultList = JacksonTemplate.toList(json, List.class);
            Map<String, Object> resultMap = resultList.get(0);
            return resultMap.get("url_short").toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 将长链接转为短链接(调用的百度短网址API),需接入相应API
     *
     * @param url
     *            需要转换的长链接url
     * @return 返回转换后的短链接
     */
    public static String convertBaiDuShortUrl(String url) {
        try {
            // 调用百度API
            HttpPost post = new HttpPost("http://www.dwz.cn/create.php");
            List<NameValuePair> params = new LinkedList<NameValuePair>();
            // 必要的url长链接参数
            params.add(new BasicNameValuePair("url", url));
            post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(post);
            // 得到调用百度API后成功返回的json字符串
            // tinyurl : 短链接地址 status:0 表示转换成功 非0表示转换失败 longurl:原始长链接地址 err_msg:错误信息
            String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8");
            Map<String, String> map = JacksonTemplate.toMap(jsonStr, String.class);
            return map.get("tinyurl").toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 测试
     * @param args
     */
    public static void main(String []args){
        //String tinyurl = convertBaiDuShortUrl("http://news.sina.com.cn/gov/xlxw/2018-09-05/doc-ihiixyeu3395739.shtml");
        System.out.println(getShortUuid());
    }

    public 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 String getShortUuid() {
        StringBuffer stringBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) { // 32 -> 8
            String str = uuid.substring(i * 4, i * 4 + 4);
            // 16进制为基解析
            int strInteger = Integer.parseInt(str, 16);
            // 0x3E -> 字典总数 62
            stringBuffer.append(chars[strInteger % 0x3E]);
        }
        return stringBuffer.toString();
    }

    /**
     * TODO 相应的需要修改的
     * @return
     */
    public static String getH5LinksPerfer() {
//        String  h5LinksPrefix = (String) Env.getProperty(H5_LINKS_PERFIX);
//        return StringUtils.isBlank(h5LinksPrefix) ? DEFAULT_H5_LINKS_PERFIX : h5LinksPrefix;

        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值