长链接转短连接

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38789941/article/details/86679586

项目中遇到的实际问题:后台编写公告发布到手机上,但是在发布之前无法确认在手机上展示效果,因为这个弊端,文宣已经骚扰我们好几次了……

本来想写记录一下,在网上一搜,比自己有文采的大有人在啊,我就不重复造轮子了,

如何将一个长URL转换为一个短URL?

这篇文章写的很好,值得阅读。

只记录一下util

package com.thinkgem.jeesite.pda.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class DwzUtil {

	 final static String CREATE_API = "https://dwz.cn/admin/v2/create";
     final static String TOKEN = "f2fd55f9275d952"; // TODO:设置Token
 
     class UrlResponse {
         @SerializedName("Code")
         private int code;
 
         @SerializedName("ErrMsg")
         private String errMsg;
 
         @SerializedName("LongUrl")
         private String longUrl;
 
         @SerializedName("ShortUrl")
         private String shortUrl;
 
         public int getCode() {
             return code;
         }
 
         public void setCode(int code) {
             this.code = code;
         }
 
         public String getErrMsg() {
             return errMsg;
         }
 
         public void setErrMsg(String errMsg) {
             this.errMsg = errMsg;
         }
 
         public String getLongUrl() {
             return longUrl;
         }
 
         public void setLongUrl(String longUrl) {
             this.longUrl = longUrl;
         }
 
         public String getShortUrl() {
             return shortUrl;
         }
 
         public void setShortUrl(String shortUrl) {
             this.shortUrl = shortUrl;
         }
     }
 
     /**
      * 创建短网址
      *
      * @param longUrl
      *            长网址:即原网址
      * @return  成功:短网址
      *          失败:返回空字符串
      */
     public static String createShortUrl(String longUrl) {
         String params = "{\"url\":\""+ longUrl + "\"}";
 
         BufferedReader reader = null;
         try {
             // 创建连接
             URL url = new URL(CREATE_API);
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
             connection.setDoOutput(true);
             connection.setDoInput(true);
             connection.setUseCaches(false);
             connection.setInstanceFollowRedirects(true);
             connection.setRequestMethod("POST"); // 设置请求方式
             connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
             connection.setRequestProperty("Token", TOKEN); // 设置发送数据的格式");
 
             // 发起请求
             connection.connect();
             OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
             out.append(params);
             out.flush();
             out.close();
 
             // 读取响应
             reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
             String line;
             String res = "";
             while ((line = reader.readLine()) != null) {
                 res += line;
             }
             reader.close();
 
             // 抽取生成短网址
             UrlResponse urlResponse = new Gson().fromJson(res, UrlResponse.class);
             if (urlResponse.getCode() == 0) {
                 return urlResponse.getShortUrl();
             } else {
                 System.out.println(urlResponse.getErrMsg());
             }
             return ""; 
         } catch (IOException e) {
             e.printStackTrace();
         }
         return ""; // TODO:自定义错误信息
     }
 
}

猜你喜欢

转载自blog.csdn.net/qq_38789941/article/details/86679586