微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

groovy常用模块

  1. http请求

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.HTTPBuilder

def urls = [
  "http://www.baidu.com",
  "http://www.163.com/" 
]

def up = urls.collect { url ->
  try {
    new HTTPBuilder( url ).get( path:'' ) { response ->
      response.statusLine.statusCode == 200
    }
  }
  catch( e ) { false }
}

println up

  1. 文件读写
https://www.w3cschool.cn/groovy/groovy_file_io.html

import java.io.File 
class Example { 
   static void main(String[] args) { 
      new File('E:/','Example.txt').withWriter('utf-8') { 
         writer -> writer.writeLine 'Hello World' 
      }  
   } 
}

  1. 检查主机是否ip
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnkNownHostException;

public class PingUtil {
    
    public static void main(String[] args) {
        String host1 = "14.215.178.37";
        String host2 = "www.baidu.com";
        ping(host1);
        ping(host2);
    }

    public static void ping(String host) {
        try {
            InetAddress inetAddress = InetAddress.getByName(host);
            boolean reachable = inetAddress.isReachable(5*1000);
            if(reachable) {
                System.out.println("ping success. Host name: " + inetAddress.getHostName() + ", IP addr: " + inetAddress.getHostAddress());
            }else {
                System.out.println("ping Failed.");
            }
        } catch (UnkNownHostException e1) {
            e1.printstacktrace();
        } catch (IOException e2) {
            e2.printstacktrace();
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐