NetUtil
来源:互联网 发布:ipad app下载软件 编辑:程序博客网 时间:2024/05/17 15:22
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.StringTokenizer;
public class NetUtil {
public static String getDefaultHostName() throws SocketException {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface nif = en.nextElement();
Enumeration<InetAddress> addresses = nif.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isLinkLocalAddress() && !address.isLoopbackAddress()) {
return address.getHostName();
}
}
}
return "localhost";
}
public static String getDefaultHostIp() throws SocketException {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while (en.hasMoreElements()) {
NetworkInterface nif = en.nextElement();
Enumeration<InetAddress> addresses = nif.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress address = addresses.nextElement();
if (!address.isLinkLocalAddress() && !address.isLoopbackAddress()) {
return address.getHostAddress();
}
}
}
return "127.0.0.1";
}
public static String getEncodedServer(String server) throws UnknownHostException {
StringTokenizer tokenizer = new StringTokenizer(server, ":");
String hostName = tokenizer.nextToken();
String port = tokenizer.nextToken();
InetAddress inetAddress = InetAddress.getByName(hostName);
byte[] rawIp = inetAddress.getAddress();
StringBuilder builder = new StringBuilder(hostName);
builder.append(":");
builder.append(BASE64Encoder.get().encode(rawIp));
builder.append(":");
builder.append(port);
return builder.toString();
}
}
/*
* BASE64Encoder.java
*
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
import java.io.OutputStream;
import java.io.IOException;
/**
* This class implements a BASE64 Character encoder as specified in RFC1521.
* This RFC is part of the MIME specification as published by the Internet
* Engineering Task Force (IETF). Unlike some other encoding schemes there
* is nothing in this encoding that indicates
* where a buffer starts or ends.
*
*/
public class BASE64Encoder extends CharacterEncoder {
private static final BASE64Encoder SINGLETON = new BASE64Encoder();
public static BASE64Encoder get() {
return SINGLETON;
}
/** this class encodes three bytes per atom. */
protected int bytesPerAtom() {
return (3);
}
/**
* this class encodes 57 bytes per line. This results in a maximum
* of 57/3 * 4 or 76 characters per output line. Not counting the
* line termination.
*/
protected int bytesPerLine() {
return (57);
}
/** This array maps the characters to their 6 bit values */
private final static char pem_array[] = {
// 0 1 2 3 4 5 6 7
'A','B','C','D','E','F','G','H', // 0
'I','J','K','L','M','N','O','P', // 1
'Q','R','S','T','U','V','W','X', // 2
'Y','Z','a','b','c','d','e','f', // 3
'g','h','i','j','k','l','m','n', // 4
'o','p','q','r','s','t','u','v', // 5
'w','x','y','z','0','1','2','3', // 6
'4','5','6','7','8','9','+','/' // 7
};
/**
* encodeAtom - Take three bytes of input and encode it as 4
* printable characters. Note that if the length in len is less
* than three is encodes either one or two '=' signs to indicate
* padding characters.
*/
protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
throws IOException {
byte a, b, c;
if (len == 1) {
a = data[offset];
b = 0;
c = 0;
outStream.write(pem_array[(a >>> 2) & 0x3F]);
outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
outStream.write('=');
outStream.write('=');
} else if (len == 2) {
a = data[offset];
b = data[offset+1];
c = 0;
outStream.write(pem_array[(a >>> 2) & 0x3F]);
outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
outStream.write('=');
} else {
a = data[offset];
b = data[offset+1];
c = data[offset+2];
outStream.write(pem_array[(a >>> 2) & 0x3F]);
outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
outStream.write(pem_array[c & 0x3F]);
}
}
public static void main(String[] args) throws Exception {
BASE64Encoder encoder = get();
System.out.println(encoder.encode("china".getBytes()));
System.out.println(encoder.encode("usa".getBytes()));
System.out.println(encoder.encode("russia".getBytes()));
System.out.println(encoder.encode("japan".getBytes()));
}
}
- NetUtil
- NetUtil
- android NetUtil
- NetUtil工具类
- 工具类NetUtil
- NETUtil IP是否可达
- AS3 NetUtil浏览器实用类
- Android工具类系列-NetUtil
- 网络请求get/post工具类:NetUtil
- 工具类--NetUtil 判断网络状态
- tornado.netutil — Miscellaneous network utilities 杂项网络工具
- 【常用工具类】NetUtil(检测当前网络状态)
- NetUtil 有关于IP端口的简单校验和操作
- wfu-pickatlas安装问题解答
- Windows Server 2003 IIS建FTP服务器
- CRichEditCtrl
- 新浪SAE架构与设计
- 使用jquery 的form的submit()方法表单不提交的原因
- NetUtil
- IMP-00032: SQL statement exceeded buffer length
- 在liferay6.1中使用调度器
- linux之awk用法
- 不要依赖hibernate的二级缓存
- 基本图像知识
- UML之用例图
- Archie osgEarth Step By Step③tif及geotiff文件格式简明分析
- 输入一个整数,输出所有相加等于这个数的算式