日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

【GitUtils】獲取gitee倉庫具體路徑下的內容

作者:six-double-seven 更新時間: 2022-05-11 編程語言

【GitUtils】獲取gitee倉庫具體路徑下的內容

1. 背景

  • GitUtils用于獲取gitee倉庫具體路徑下的內容;
  • 一些簡單參數及字段的存儲和讀取如果建表會顯得過于臃腫,讀取倉庫中實時更新的內容顯然更合適。

2. 實現

2.1 readFile()

  • 使用okhttp3發送request,相關參數如下。
參數 含義 備注
accessToken 用戶授權碼 若無,則傳null;
owner 用戶名 例如 “six-double-seven”
repo 倉庫名稱 例如 “blog”
path 文件路徑 以反斜杠 / 開頭,例如 “/tools/GitUtil/微塵.md”
ref 分支 默認是倉庫的默認分支

2.2 convertData()

  • 完成數據格式的轉換,Base64轉String;
  • 第一步:Base64轉byte[];
  • 第二步:byte[]轉String。

2.3 GitUtils.java

  • 以gitee為例,GitUtils.java如下。
package utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.Test;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Base64;

public class GitUtils {

    /**
     * @param accessToken 用戶授權碼
     * @param owner 用戶名
     * @param repo  倉庫名稱
     * @param path  文件路徑【以反斜杠 / 開頭】
     * @param ref   分支(默認是倉庫的默認分支)
     * @return
     * @throws Exception
     */
    public static String readFile(String accessToken, String owner, String repo, String path, String ref) throws Exception {

        String gitUrl = "https://gitee.com/api/v5/repos/";
        StringBuffer url = new StringBuffer(gitUrl);
        url.append(owner)
            .append("/")
            .append(repo)
            .append("/contents")
            .append(path);
        if (accessToken != null)
            url.append("access_token=")
            .append(accessToken);
        if (ref != null)
            url.append("&ref=")
            .append(ref);
        Request request = new Request.Builder()
            .url(url.toString())
            .build();

        String resStr = null;
        try {
            OkHttpClient client = new OkHttpClient
                .Builder()
                .build();

            Call call = client.newCall(request);
            Response response = call.execute();
            resStr = response.body().string();

        } catch (IOException e) {
            e.printStackTrace();
        }
        //數據格式轉換: Base64轉String
        return convertData(resStr);
    }

    private static String convertData(String resStr) {

        JSONObject jsonObject = JSON.parseObject(resStr);
        byte[] content = null;
        try {
            //1. Base64轉byte[]
            content = Base64.getDecoder().decode(jsonObject.getString("content"));
        } catch (Exception e) {
            System.out.println(">> response is " + resStr);
        }
        String contentStr = null;
        if (content != null) {
            try {
                //2. byte[]轉String
                contentStr = new String(content, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return contentStr;
    }
    @Test
    public void getWeiChen() throws Exception {
        String s = GitUtils.readFile(null, "six-double-seven", "blog", "/tools/GitUtil/微塵.md", null);
        System.out.println(s);
    }
}

原文鏈接:https://blog.csdn.net/qq_52641681/article/details/124662820

欄目分類
最近更新