本地导入文件,以省市区为例:
@PostMapping(value = "/contryImport")
public void contryImport(String data) throws IOException {
//文件放到桌面啦
String file = "C:\\Users\\xxx\\Desktop\\country.json";
InputStream inputStream = new FileInputStream(new File(file));
byte[] bytes = new byte[inputStream.available()];
StringBuffer buffer = new StringBuffer();
//读取文件
inputStream.read(bytes);
buffer.append(new String(bytes, Charsets.UTF_8));
inputStream.close();
List<SysRegion> SysRegions = new ArrayList<>();
//解析JSON文件
JSONObject json = JSON.parseObject(buffer.toString());
JSONArray jsonArray = json.getJSONArray("features");
//JSONArray jsonArray = JSONArray.parseArray(buffer.toString());
int j =1;
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// 处理jsonObject
System.out.println(jsonObject);
JSONObject properties = jsonObject.getJSONObject("properties");
String name = properties.getString("name");
String iso_a3 = properties.getString("iso_a3");
String full_name = properties.getString("full_name");
SysRegion SysRegion = new SysRegion();
SysRegion.setFlag("1");
SysRegion.setName(name);
SysRegion.setCode(iso_a3);
int i1 = j++;
SysRegion.setOrderNum(i1+"");
SysRegion.setDescription(full_name);
SysRegion.setDicType("country");
SysRegions.add(SysRegion);
}
if(CollectionUtil.isNotEmpty(SysRegions)){
SysRegionService.saveBatch(SysRegions);
}
}