有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (3) 个答案

  1. # 1 楼答案

    我们可以在Java中执行curl命令,并使用基本身份验证来访问文件

    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;
    
            }
    
  2. # 2 楼答案

    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";
        }
    }
    
  3. # 3 楼答案

    这是一种简单的java方式。。。 我用的是标准java。网URL api和Base64类连接github并打印json,如以下示例所示:

    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");
            }
        }
    }