首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >短网址生成相关

短网址生成相关

作者头像
Dream城堡
发布2020-11-11 17:36:47
2K0
发布2020-11-11 17:36:47
举报
文章被收录于专栏:Spring相关Spring相关Spring相关
/***
 * 短链接转换工具类
 *
 * @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;
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档