我正在嘗試讀取 GitHub URL 的特定檔案夾下的檔案和每個檔案的原始內容,但我沒有得到正確的回應。
基本上,我想做這樣的事情:
我有一個公共 GitHub 專案,其中有一個檔案夾
folder1。這folder1也包含許多檔案XML/JSON。我想使用指向
folder1:的鏈接https://github.com/comapny/project/tree/master/folder1/tempFolder并獲取其內容。在這個檔案夾下,我有許多檔案,我想讀取其內容的原始資料。
截至目前,我能夠對檔案夾下的單個檔案及其原始資料提出請求,但無法讀取檔案夾下的檔案并讀取所有檔案及其內容。
以下是我到目前為止的代碼:
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class TestMain {
public static void main(String[] args) throws IOException {
//Read the GitHub files
final String inputURL = "https://github.com/company/project/tree/master/XML/withData";
//final String inputURL = "https://raw.githubusercontent.com/company/project/blob/master/XML/withData/myFileName.xml";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(new HttpGet(inputURL));
StatusLine statusLine = response.getStatusLine();
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
System.out.println("Response Code : " statusLine.getStatusCode() " ---- " "Response Phrase : " statusLine.getReasonPhrase());
System.out.println("Response body: " responseBody);
}
}
uj5u.com熱心網友回復:
您可以使用GitHub 的存盤庫內容 API列出目錄中的檔案以及讀取檔案內容。
例如,列出名為“XML”的目錄中的檔案可能如下所示:
https://api.github.com/repos/company/project/contents/XML/
回應將是目錄中的專案陣列。每個專案都包含屬性type,它可以讓您知道專案是檔案還是目錄,以及url請求該專案的鏈接。
通過對專案的 URL 執行新請求,您可以通過其content屬性訪問檔案的內容,或者如果它是目錄,則可以訪問其中的另一個檔案陣列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/460272.html
標籤:爪哇 文件 github github-api
