解决github图片不能正常显示问题

解决github图片不能正常显示问题

解决办法

  • 解决办法有很多,网上都有参考,例如解决Github网页上图片显示失败的问题;
  • 但是这些办法都需要手动一个个的去记录,然后复制到hosts文件中去;
  • 我写了一个java程序,能够自动获取所有的domain对应的ip,运行程序即可一次性获得所有ip,下次失效,再次运行一遍即可;
  • 项目地址在github-dns-parser,需要自取;
  • 核心代码如下:
@Slf4j
public class CrawlTask implements Runnable {
     


    private final HttpClient httpClient;

    private final String url;

    public CrawlTask(HttpClient httpClient, String url) {
     
        this.httpClient = httpClient;
        this.url = url;
    }

    @Override
    public void run() {
     
        try {
     
            HttpResponse response = httpClient.execute(new HttpGet(url));
            HttpEntity entity = response.getEntity();
            String body = EntityUtils.toString(entity, "utf-8");
            Host host = parseDocument(body);
            log.info("{} {}", host.getIpv4(), host.getDomain());
        } catch (Exception e) {
     
            log.error("error occur while parse {}", url, e);
        }
    }


    private Host parseDocument(String body) {
     
        Document doc = Jsoup.parse(body);
        String[] split = url.split("/");
        String ipv4 = doc.select("table > tbody > tr:nth-child(2) > td > ul > li").html();
        return new Host(split[split.length-1], ipv4);
    }
public class DnsParser {
     

    private static final PoolingHttpClientConnectionManager MANAGER = new PoolingHttpClientConnectionManager();

    private static final CloseableHttpClient HTTP_CLIENT = HttpClients.custom()
            .setConnectionManager(MANAGER)
            .build();

    private static final String[] DOMAINS = {
     "https://github.com.ipaddress.com/www.github.com",
            "https://githubusercontent.com.ipaddress.com/raw.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/gist.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/cloud.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/camo.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars0.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars1.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars2.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars3.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars4.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars5.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars6.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars7.githubusercontent.com" ,
            "https://githubusercontent.com.ipaddress.com/avatars8.githubusercontent.com",
            "https://githubusercontent.com.ipaddress.com/avatars.githubusercontent.com"};


    public static void main(String[] args){
     
        System.out.println("#GitHub start");
        ExecutorService executorService = Executors.newFixedThreadPool(DOMAINS.length);
        for (String domain : DOMAINS) {
     
            executorService.execute(new CrawlTask(HTTP_CLIENT, domain));
        }
        executorService.shutdown();
        while (true) {
     
            if(executorService.isTerminated()) {
     
                break;
            }
        }
        System.out.println("#GitHub end");
    }


}

你可能感兴趣的:(实用,github,java,httpclient)