I want to store a file in Github as a string in java and process it later in the code. How can I do that ?
Asked
Active
Viewed 9,061 times
2
-
3Use a library to access the git repo? – Kayaman May 16 '18 at 10:23
-
@Kayaman isn't github providing it's own api anyway ? – N.K May 16 '18 at 10:28
-
This can probably just be a single HTTP GET call. – Thilo May 16 '18 at 10:31
-
@xoxel use a HTTP library to access the api? :) – Kayaman May 16 '18 at 10:33
-
@Kayaman that's not what i meant (i was ironic) ^^, i meant that Github IS providing an api anyways, right ? so it should be easy enough to do that with their own api :) And of course you can use an HTTP lib, HTTPClient is decently easy to use aswell ^^ – N.K May 16 '18 at 10:36
-
The file is in a private repository https:bitbucket.com.......I am just beginning with these. Could you please tell whick library needs to be downloaded and where to get it @Kayaman ? – Ankit May 16 '18 at 10:42
-
Since you're just beginning, you have plenty of time to google around, read other related questions on SO, etc. When you have a specific problem, then you can ask a question about it. As it is, this question is too broad. – Kayaman May 16 '18 at 10:44
-
My problem is that there is .json file in bitbucket and I need to get it in String. How can that be achieved ? – Ankit May 16 '18 at 10:47
-
@Ankit for the JSON handling take a look at JSONObject and JSONArray, those two are provided by Java and there is plenty of tutorial and high quality learning material on the web. – N.K May 16 '18 at 10:55
-
@xoxel i wanted to know how to retrieve a file from a private github repository that requires a username and password to login. Is it using HTTPClient() or GitHubClient() and how to do it ? – Ankit May 16 '18 at 11:03
-
Maybe this helps (not Java, but you can adapt): https://stackoverflow.com/questions/18126559/how-can-i-download-a-single-raw-file-from-a-private-github-repo-using-the-comman – Thilo May 16 '18 at 11:09
-
thilo's link aswell as HTTPClient and github's api / any library that can replace it, that's all you need to start your own research :) – N.K May 16 '18 at 11:40
-
I tried using HTTPClient. It just gives the page source that does not contain the file. Github's api ? – Ankit May 16 '18 at 11:43
-
You don't know how to use it that's different, you need to use get/post requests in order to retrieve the data from Github, if you know what's an API, then is referred as the "github's API" the server side applications which respond to your get and post requests, but you need to call it using such requests. That's like ordering food, you order (get request) to a fast-food (API) which then give you your meal back as a result (response) – N.K May 16 '18 at 13:08
-
Basic authentication for http and https connection is not working(https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_8.7.0/com.ibm.swg.im.iis.ia.restapi.doc/topics/r_restapi_sending_https_java.html). I have looked for Api for bitbucket and couldn't find one – Ankit May 16 '18 at 13:43
3 Answers
2
We can execute curl commands in Java and use Basic authentication to access files.
URL url;String username="username",password="password",file="";
try {
url = new URL("https://www.bitbucket.com/raw-file-url");
URLConnection uc;
uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
ArrayList<String> list=new ArrayList<String>();
String userpass = username + ":" + password;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));//needs Base64 encoder, apache.commons.codec
uc.setRequestProperty("Authorization", basicAuth);
BufferedReader reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
file=file+line+"\n";
System.out.println(file);
return file;
} catch (IOException e) {
System.out.println("Wrong username and password");
return null;
}

Ankit
- 69
- 1
- 6
-
Store password in a mutable container (`char []` for example) and clear it after use. – Gyapti Jain May 29 '18 at 06:30
1
This is plain java way of doing this... I used standard java.net.URL api and Base64 class to connect github and print a json like the below example :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Base64;
public class GitConnect {
public static void main(String... args) throws Exception {
java.net.URL url = null;
String username = "user";
String password = "gitpwd";
String file = "";
try {
url = new java.net.URL("https://raw.githubusercontent.com/lrjoshi/webpage/master/public/post/c159s.csv");
java.net.URLConnection uc;
uc = url.openConnection();
uc.setRequestProperty("X-Requested-With", "Curl");
java.util.ArrayList<String> list = new java.util.ArrayList<String>();
String userpass = username + ":" + password;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userpass.getBytes()));//needs Base64 encoder, apache.commons.codec
uc.setRequestProperty("Authorization", basicAuth);
BufferedReader reader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
file = file + line + "\n";
System.out.println(file);
} catch (IOException e) {
System.out.println("Wrong username and password");
}
}
}

Ram Ghadiyaram
- 28,239
- 13
- 95
- 121
0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
public static String getTextFromGithub(String link) {
URL Url = null;
try {
Url = new URL(link);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
HttpURLConnection Http = null;
try {
Http = (HttpURLConnection) Url.openConnection();
} catch (IOException e1) {
e1.printStackTrace();
}
Map<String, List<String>> Header = Http.getHeaderFields();
for (String header : Header.get(null)) {
if (header.contains(" 302 ") || header.contains(" 301 ")) {
link = Header.get("Location").get(0);
try {
Url = new URL(link);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
Http = (HttpURLConnection) Url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
Header = Http.getHeaderFields();
}
}
InputStream Stream = null;
try {
Stream = Http.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
String Response = null;
try {
Response = GetStringFromStream(Stream);
} catch (IOException e) {
e.printStackTrace();
}
return Response;
}
private static String GetStringFromStream(InputStream Stream) throws IOException {
if (Stream != null) {
Writer Writer = new StringWriter();
char[] Buffer = new char[2048];
try {
Reader Reader = new BufferedReader(new InputStreamReader(Stream, "UTF-8"));
int counter;
while ((counter = Reader.read(Buffer)) != -1) {
Writer.write(Buffer, 0, counter);
}
} finally {
Stream.close();
}
return Writer.toString();
} else {
return "No Contents";
}
}

Nathan Shin
- 1
- 1