Created
July 17, 2015 02:27
-
-
Save hizhengfu/0522443e7dd6c4b2d764 to your computer and use it in GitHub Desktop.
读取行政区划码入库
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
public String submitArea() { | |
String name = "area.txt"; | |
try { | |
List<String> areas = FileUtils.readLines(new File(Global.getProjectPath() + "/" + name)); | |
Area area, parea = null, rootarea = new Area("1"); | |
rootarea.setParentIds("0,"); | |
String pcode = "", tcode = ""; | |
for (String a : areas) { | |
area = new Area(); | |
String _a[] = a.split("[\\s ]+"); | |
if (_a.length == 2) { | |
String code = StringUtils.trim(_a[0]); | |
area.setCode(code); | |
area.setName(StringUtils.trim(_a[1]).replace(" ", "")); | |
//直辖市,省 | |
if ("0000".equals(code.substring(2))) { | |
area.setType("2"); | |
parea = rootarea; | |
} else if ("00".equals(code.substring(4))) {//市 | |
area.setType("3"); | |
tcode = code.substring(0, 2) + "0000"; | |
} else {//县 | |
area.setType("4"); | |
tcode = code.substring(0, 4) + "00"; | |
} | |
if (!pcode.equals(tcode)) { | |
pcode = tcode; | |
parea = areaService.findByCode(pcode); | |
} | |
area.setParent(parea); | |
if (areaService.findByCode(code) == null) { | |
areaService.save(area); | |
} | |
} | |
} | |
} catch (Exception e) { | |
return "读文件错误"; | |
} | |
return "成功"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment