【GitUtils】获取gitee仓库具体路径下的内容

【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);
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值