Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhe.wang
committed
Mar 17, 2016
0 parents
commit 6deb2be
Showing
11 changed files
with
267,758 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.ggstar</groupId> | ||
<artifactId>ipdatabase</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.11</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.poi</groupId> | ||
<artifactId>poi-ooxml</artifactId> | ||
<version>3.14</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>log4j</groupId> | ||
<artifactId>log4j</artifactId> | ||
<version>1.2.17</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.poi</groupId> | ||
<artifactId>poi</artifactId> | ||
<version>3.14</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.ggstar.util.file; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.BufferedReader; | ||
import java.io.FileInputStream; | ||
import java.io.InputStreamReader; | ||
|
||
/** | ||
* Created by lee on 2015/7/29. | ||
*/ | ||
public class FileUtil { | ||
|
||
private static final String UTF_8 = "UTF-8"; | ||
|
||
private static final String Unicode = "Unicode"; | ||
|
||
private static final String UTF_16BE = "UTF-16BE"; | ||
|
||
private static final String ANSI_ASCII = "ANSI|ASCII"; | ||
|
||
private static final String GBK = "GBK"; | ||
|
||
|
||
|
||
/** | ||
* 自动根据文件编码格式读取文件 | ||
* | ||
* @param filePath | ||
* @return | ||
* @throws Exception | ||
*/ | ||
public static BufferedReader readFile(String filePath) throws Exception { | ||
return new BufferedReader(new InputStreamReader(new FileInputStream(filePath), codeString(filePath))); | ||
} | ||
|
||
public static String codeString(String fileName) throws Exception { | ||
BufferedInputStream bin = new BufferedInputStream( | ||
new FileInputStream(fileName)); | ||
int p = (bin.read() << 8) + bin.read(); | ||
String code; | ||
//其中的 0xefbb、0xfffe、0xfeff、0x5c75这些都是这个文件的前面两个字节的16进制数 | ||
switch (p) { | ||
case 0xefbb: | ||
code = UTF_8; | ||
break; | ||
case 0xfffe: | ||
code = Unicode; | ||
break; | ||
case 0xfeff: | ||
code = UTF_16BE; | ||
break; | ||
case 0x5c75: | ||
code = ANSI_ASCII; | ||
break; | ||
default: | ||
code = GBK; | ||
} | ||
|
||
return code; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.ggstar.util.file; | ||
|
||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.apache.poi.ss.usermodel.WorkbookFactory; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Created by lee on 2015/8/11. | ||
*/ | ||
public class PoiUtil { | ||
|
||
public static Workbook getWorkbook(String filePath){ | ||
try { | ||
return WorkbookFactory.create(new FileInputStream(filePath)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} catch (InvalidFormatException e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.ggstar.util.ip; | ||
|
||
import com.ggstar.util.file.FileUtil; | ||
import com.ggstar.util.file.PoiUtil; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
|
||
import java.io.BufferedReader; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by lee on 2015/8/11. | ||
*/ | ||
public class IpHelper { | ||
|
||
private static IpTree ipTree = IpTree.getInstance(); | ||
|
||
private static final String ipFile = "ipDatabase.csv"; | ||
|
||
private static final String regionFile = "ipRegion.xlsx"; | ||
|
||
static{ | ||
buildTrain(); | ||
} | ||
|
||
private static void buildTrain() { | ||
List<IpRelation> ipRelationList; | ||
try { | ||
ipRelationList = IpHelper.getIpRelation(); | ||
int count = 0; | ||
for (IpRelation ipRelation : ipRelationList) { | ||
System.out.println(ipRelation.getIpStart() + "," + ipRelation.getIpEnd() + "," + ipRelation.getProvince()); | ||
ipTree.train(ipRelation.getIpStart(), ipRelation.getIpEnd(), ipRelation.getProvince()); | ||
if(count > 10){ | ||
break; | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
public static String findRegionByIp(String ip){ | ||
return ipTree.findIp(ip); | ||
} | ||
|
||
public static List<IpRelation> getIpRelation() throws Exception { | ||
|
||
// <ipCode, province> | ||
Map<Integer, String> regionRelationMap = getRegionRelationMap(); | ||
String file = IpHelper.class.getClassLoader().getResource(ipFile).getFile(); | ||
BufferedReader ipRelationReader = FileUtil.readFile(file); | ||
|
||
String line; | ||
List<IpRelation> list = new ArrayList<IpRelation>(); | ||
while((line = ipRelationReader.readLine()) != null){ | ||
String[] split = line.split(","); | ||
String ipStart = split[0]; | ||
String ipEnd = split[1]; | ||
Integer ipCode = Integer.valueOf(split[2]); | ||
|
||
String province = regionRelationMap.get(ipCode); | ||
IpRelation ipRelation = new IpRelation(); | ||
ipRelation.setIpStart(ipStart); | ||
ipRelation.setIpEnd(ipEnd); | ||
ipRelation.setProvince(province); | ||
list.add(ipRelation); | ||
} | ||
return list; | ||
|
||
} | ||
|
||
/** | ||
* @return Map<ipCode, province> | ||
* @throws Exception | ||
*/ | ||
public static Map<Integer, String> getRegionRelationMap() throws Exception { | ||
// BufferedReader ipRelationReader = FileUtil.readFile(regionFile); | ||
String file = IpHelper.class.getClassLoader().getResource(regionFile).getFile(); | ||
|
||
System.out.println(file); | ||
|
||
|
||
Workbook workbook = PoiUtil.getWorkbook(file); | ||
|
||
Sheet sheet = workbook.getSheetAt(0); | ||
Map<Integer, String> map = new HashMap<Integer, String>(); | ||
int rowLen = sheet.getPhysicalNumberOfRows(); | ||
for (int i = 1; i < rowLen; i++) { | ||
Row row = sheet.getRow(i); | ||
String province = row.getCell(0).getStringCellValue(); | ||
Double a = row.getCell(2).getNumericCellValue(); | ||
Integer ipCode = a.intValue(); | ||
map.put(ipCode, province); | ||
} | ||
|
||
// String line; | ||
// Map<Integer, String> map = new HashMap<>(); | ||
// ipRelationReader.readLine(); | ||
// while((line = ipRelationReader.readLine()) != null){ | ||
// String[] split = line.split("\t"); | ||
// String province = split[0]; | ||
// Integer ipCode = Integer.valueOf(split[2]); | ||
// map.put(ipCode, province); | ||
// } | ||
return map; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.ggstar.util.ip; | ||
|
||
/** | ||
* Created by lee on 2015/8/11. | ||
*/ | ||
public class IpRelation { | ||
|
||
private String ipStart; | ||
|
||
private String ipEnd; | ||
|
||
private int ipCode; | ||
|
||
private String province; | ||
|
||
private String city; | ||
|
||
public String getCity() { | ||
return city; | ||
} | ||
|
||
public void setCity(String city) { | ||
this.city = city; | ||
} | ||
|
||
public int getIpCode() { | ||
return ipCode; | ||
} | ||
|
||
public void setIpCode(int ipCode) { | ||
this.ipCode = ipCode; | ||
} | ||
|
||
public String getIpEnd() { | ||
return ipEnd; | ||
} | ||
|
||
public void setIpEnd(String ipEnd) { | ||
this.ipEnd = ipEnd; | ||
} | ||
|
||
public String getIpStart() { | ||
return ipStart; | ||
} | ||
|
||
public void setIpStart(String ipStart) { | ||
this.ipStart = ipStart; | ||
} | ||
|
||
public String getProvince() { | ||
return province; | ||
} | ||
|
||
public void setProvince(String province) { | ||
this.province = province; | ||
} | ||
} |
Oops, something went wrong.