欢迎光临
我们一直在努力

根据IP找地址的java实现-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

根据ip找地址的java实现,能将\t分隔的ip,地址格式(起始ip\t结束ip\t国家\t地区\n)转化为升序的二进制格式,通过二分查找能在50-150ms内找到对应ip的地址信息.

原始数据可用iplook生成. 代码:

iptool.java

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

/*

* 创建日期 2004-10-23

*

*/

package starfire.proxy.ip;

import java.io.bufferedreader;

import java.io.dataoutputstream;

import java.io.file;

import java.io.fileinputstream;

import java.io.fileoutputstream;

import java.io.ioexception;

import java.io.inputstreamreader;

import java.io.randomaccessfile;

import java.net.inetaddress;

import java.net.unknownhostexception;

import java.util.arraylist;

import java.util.collections;

import java.util.comparator;

import java.util.hashmap;

import java.util.iterator;

import java.util.stringtokenizer;

/**

* 4|4|4|4|4#总纪录数|ip偏移量|索引偏移量|数据偏移量

* (4|4|2|2)*#起始ip|结束ip|国家索引|区域索引

* (2|4)*#2位索引,4位偏移量(指向对应country|local开始位置) (countrynull|localnull)*

*

* @author starfire

*

*/

public class iptool {

private string datapath;

private randomaccessfile raf;

private ipfile ipfile;

private byte[] buffer = new byte[40];

private ipinfo mininfo = new ipinfo();

private ipinfo maxinfo = new ipinfo();

public static int str2ip(string ip) throws unknownhostexception {

inetaddress address = inetaddress.getbyname(ip);

byte[] bytes = address.getaddress();

int a, b, c, d;

a = byte2int(bytes[0]);

b = byte2int(bytes[1]);

c = byte2int(bytes[2]);

d = byte2int(bytes[3]);

int result = (a << 24) | (b << 16) | (c << 8) | d;

return result;

}

public static int byte2int(byte b) {

int l = b & 0x07f;

if (b < 0) {

l |= 0x80;

}

return l;

}

public static void format(string datapath, string targetpath)

throws ioexception {

long time = system.currenttimemillis();

bufferedreader in = new bufferedreader(new inputstreamreader(

new fileinputstream(datapath)));

fileoutputstream fos = new fileoutputstream(targetpath);

dataoutputstream out = new dataoutputstream(fos);

string line;

arraylist iplist = new arraylist(150000);

hashmap map = new hashmap(65535);

arraylist datalist = new arraylist(65535);

iterator iterator = null;

while ((line = in.readline()) != null) {

stringtokenizer token = new stringtokenizer(line, "\t");

string start;

string end;

string country;

string local;

if (token.counttokens() == 4) {

start = token.nexttoken();

end = token.nexttoken();

country = token.nexttoken().trim();

local = token.nexttoken();

if (local.startswith("/")) {

local = local.substring(1);

}

local = local.trim();

try {

iporigininfo info = new iporigininfo();

info.startip = str2ip(start);

info.endip = str2ip(end);

info.country = country;

info.local = local;

iplist.add(info);

object obj = map.put(country, new integer(map.size()));

if (obj!=null) {

map.put(country,obj);

}

else {

datalist.add(country);

}

obj = map.put(local, new integer(map.size()));

if (obj!=null) {

map.put(local,obj);

}

else {

datalist.add(local);

}

} catch (unknownhostexception e) {

continue;

}

}

}

ipfile ipfile = new ipfile();

ipfile.totalrecords = iplist.size();

ipfile.ipoffset = ipfile.header_size;

ipfile.indexoffset = ipfile.ipoffset + iplist.size()

* ipfile.ip_info_size;

ipfile.dataoffset = ipfile.indexoffset + map.size() * ipfile.index_size;

system.out.println("totalrecords:" + ipfile.totalrecords);

system.out.println("ipoffset:" + ipfile.ipoffset);

system.out.println("indexoffset:" + ipfile.indexoffset);

system.out.println("dataoffset:" + ipfile.dataoffset);

system.out.println("data size:" + map.size());

//system.out.println("total size:"+total);

out.writeint(ipfile.magic);

out.writeint(ipfile.totalrecords);

out.writeint(ipfile.ipoffset);

out.writeint(ipfile.indexoffset);

out.writeint(ipfile.dataoffset);

out.flush();

//排序

collections.sort(iplist, new comparator() {

public int compare(object o1, object o2) {

iporigininfo info1 = (iporigininfo) o1;

iporigininfo info2 = (iporigininfo) o2;

long start = int2long(info1.startip);

long end = int2long(info2.startip);

return (int) (start – end);

}

});

//写ip信息

iterator = iplist.iterator();

while (iterator.hasnext()) {

iporigininfo info = (iporigininfo) iterator.next();

out.writeint(info.startip);

out.writeint(info.endip);

short s = ((integer) map.get(info.country)).shortvalue();

out.writeshort(s);

s = ((integer) map.get(info.local)).shortvalue();

out.writeshort(s);

}

out.flush();

//写索引信息

int position = ipfile.dataoffset;

int num = 0;

iterator = datalist.iterator();

while (iterator.hasnext()) {

string value = (string) iterator.next();

out.writeshort(num++);

out.writeint(position);

if (position > 2729877) {

system.err.println("error:" + position);

}

position += value.getbytes().length + 1;

}

out.flush();

//写country|local数据

//iterator = sort.iterator();

int size = datalist.size();

for (int i = 0; i < size; i++) {

string value = (string) datalist.get(i);

out.write(value.getbytes());

out.write((byte) 0);

}

out.flush();

in.close();

out.close();

system.out.println("used " + (system.currenttimemillis() – time) / 1000

+ " seconds");

}

public iptool(string datapath) throws ioexception {

this.datapath = datapath;

this.ipfile = new ipfile();

load(datapath);

}

void load(string datapath) throws ioexception {

file f = new file(datapath);

raf = new randomaccessfile(f, "r");

int magic = raf.readint();

if (magic != ipfile.magic) {

throw new ioexception("bad format,magic number not match");

}

ipfile.totalrecords = raf.readint();

ipfile.ipoffset = raf.readint();

ipfile.indexoffset = raf.readint();

ipfile.dataoffset = raf.readint();

ipfile.datacount = (ipfile.dataoffset – ipfile.indexoffset)

/ ipfile.index_size;

if (raf.length() < ipfile.dataoffset) {

throw new ioexception("bad format,length not match");

}

ipfile.ipcount = (ipfile.indexoffset – ipfile.ipoffset)

/ ipfile.ip_info_size;

ipfile.minip = int2long(raf.readint());

raf.seek(ipfile.indexoffset – ipfile.ip_info_size);

ipfile.maxip = int2long(raf.readint());

//system.out.println(ipfile.ipcount);

//fillinfo(0,mininfo);

//fillinfo(ipfile.ipcount-1,maxinfo);

}

private void fillinfo(int pos, ipinfo info) throws ioexception {

raf.seek(ipfile.ipoffset + pos * ipfile.ip_info_size + 8);

int ci = short2int(raf.readshort());

int li = short2int(raf.readshort());

raf.seek(ipfile.indexoffset + ci * ipfile.index_size + 2);

ci = raf.readint();

raf.seek(ipfile.indexoffset + li * ipfile.index_size + 2);

li = raf.readint();

long cl = int2long(ci);

long ll = int2long(li);

//system.out.println("ci=" + cl + " li=" + ll);

raf.seek(cl);

int ret = raf.read(buffer);

int i = 0;

for (; i < ret; i++) {

if (buffer[i] == 0) {

break;

}

}

info.setcountry(new string(buffer, 0, i));

raf.seek(ll);

ret = raf.read(buffer);

for (i = 0; i < ret; i++) {

if (buffer[i] == 0) {

break;

}

}

info.setlocal(new string(buffer, 0, i));

//system.out.println(ll+" length="+raf.length());

}

public synchronized ipinfo find(string ip) {

ipinfo info = new ipinfo();

info.setip(ip);

try {

//long time = system.currenttimemillis();

long ipnum = ip2long(ip);

int pos = -1;

if (ipnum == ipfile.minip) {

pos = 0;

} else if (ipnum == ipfile.maxip) {

pos = ipfile.ipcount – 1;

} else {

pos = find(0, ipfile.ipcount – 1, ipnum);

}

if (pos != -1) {

fillinfo(pos, info);

}

//system.out.println("time="+(system.currenttimemillis()-time));

} catch (unknownhostexception e) {

} catch (ioexception e) {

e.printstacktrace();

}

return info;

}

public int find(int start, int end, long target) throws ioexception {

if (start >= end) {

return -1;

}

int middle = (end + start) / 2;

raf.seek(ipfile.ipoffset + middle * ipfile.ip_info_size);

long i = int2long(raf.readint());

long j = int2long(raf.readint());

if (target >= i && target <= j) {

return middle;

}

if (target < i) {

end = middle;

} else {

start = middle;

}

return find(start, end, target);

}

public static long ip2long(string ip) throws unknownhostexception {

int ipnum = str2ip(ip);

return int2long(ipnum);

}

public static long int2long(int i) {

long l = i & 0x7fffffffl;

if (i < 0) {

l |= 0x080000000l;

}

return l;

}

public static int short2int(short s) {

int i = s & 0x7fff;

if (s < 0) {

s |= 0x08000;

}

return s;

}

protected void finalize() {

if (raf != null) {

try {

raf.close();

} catch (ioexception e) {

}

}

}

public static void main(string[] args) throws exception {

//int ip = str2ip("192.168.0.1");

//system.out.println(ip2long(ip));

//format("ipdata.txt", "result.dat");

iptool tool = new iptool("result.dat");

ipinfo info = tool.find("211.155.224.222");

system.out.println(info.getcountry() + " " + info.getlocal());

}

}

class iporigininfo {

public int startip;

public int endip;

public string country;

public string local;

}

class ipfile {

public static final int magic = 0x05080321;

public static final int header_size = 20;

public static final int ip_info_size = 12;

public static final int index_size = 6;

public final int magic = magic;

public int totalrecords;

public int ipoffset = header_size;

public int indexoffset;

public int dataoffset;

int datacount;

int ipcount;

long minip;

long maxip;

}

ipinfo.java

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

/*

* 创建日期 2004-10-23

*

*/

package starfire.proxy.ip;

/**

* @author starfire

*

*/

public class ipinfo {

private string ip;

private string country = "";

private string local = "";

/**

* @return 返回 country。

*/

public string getcountry() {

return country;

}

/**

* @param country 要设置的 country。

*/

public void setcountry(string country) {

this.country = country;

}

/**

* @return 返回 ip。

*/

public string getip() {

return ip;

}

/**

* @param ip 要设置的 ip。

*/

public void setip(string ip) {

this.ip = ip;

}

/**

* @return 返回 local。

*/

public string getlocal() {

return local;

}

/**

* @param local 要设置的 local。

*/

public void setlocal(string local) {

this.local = local;

}

}

用iptool的format(string datapath, string targetpath)可将原始数据文件转化为内部使用格式.

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 根据IP找地址的java实现-JSP教程,Java技巧及代码
分享到: 更多 (0)