- 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
- 文件读写
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'
}
}
}
- 检查主机是否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();
}
}
}