網站首頁 編程語言 正文
【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
- 上一篇:springboot多版本管理
- 下一篇:linq中的串聯操作符_實用技巧
相關推薦
- 2022-07-12 解決錯誤:org.apache.ibatis.binding.BindingException
- 2023-07-26 TypeScript中的聯合類型、類型別名、接口、類型斷言
- 2022-08-10 pandas數據清洗實現刪除的項目實踐_python
- 2022-10-10 Android開發之permission動態權限獲取詳解_Android
- 2022-06-10 AJAX原理以及axios、fetch區別實例詳解_AJAX相關
- 2023-12-10 Failed to process, please exclude the tableName or
- 2022-11-29 Android?Jetpack組件DataBinding詳解_Android
- 2022-04-26 Python?Socket?編程知識點詳細介紹_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支