在读取的时候 加入了 线程池
BaiduApi
/**
* @author 获取经纬度(地址匹配经纬度需要时间)
*/
public class BaiduApi {
/**
* Baidu地图通过地址获取经纬度
*/
public static String getLngAndLat(String address) {
String location = "";
address = address.replace(" ", "");
String urls="http://api.map.baidu.com/geocoding/v3/?output=json&ak=VsOaLOgYEY55vjviQL17qtWIcmWrkhkn&address96="+address;
try {
String json = loadJSON(urls);
JSONObject obj = JSONObject.parseObject(json);
if (obj.get("status").toString().equals("0")) {
// 设置保留小数位数
java.text.DecimalFormat df =new java.text.DecimalFormat("#.00000");
// 经度
double lng = obj.getJSONObject("result").getJSONObject("location").getDouble("lng");
String newLng=df.format(lng);
// 纬度
double lat = obj.getJSONObject("result").getJSONObject("location").getDouble("lat");
String newLat=df.format(lat);
System.err.println(newLng);
location = lng+","+lat;
} else {
System.out.println("未找到相匹配的经纬度!");
}
} catch (Exception e) {
System.out.println("未找到相匹配的经纬度,请检查地址!");
}
return location;
}
public static String loadJSON(String url) {
StringBuilder json = new StringBuilder();
try {
URL oracle = new URL(url);
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream(), "UTF-8"));
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return json.toString();
}
// public static void main(String[] args) {
// String location = getLngAndLat("四川省-资阳市-安岳县-东方红小学南(西大街北)152-4号");
// System.out.println(location);
// }
}
TestReadFile
public class TestReadFile {
public static void readColumn(File file, int index) throws Exception {
InputStream inputStream = new FileInputStream(file.getAbsoluteFile());
Workbook workbook = Workbook.getWorkbook(inputStream);
Sheet sheet = workbook.getSheet(0);
int rows = sheet.getRows();// 行
int columns = sheet.getColumns();// 列
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("readFile-pool-%d").build();
ExecutorService exec = new ThreadPoolExecutor(5, 20,
0L, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue<>(20), namedThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
List<String> locationList = new ArrayList<>();
// 从第几行开始
for (int i = 0; i < rows; i++) {
Cell cell = sheet.getCell(index, i);
locationList.add(cell.getContents());
// 读取经纬度
// BaiduApi.getLngAndLat(cell.getContents());
// 读取地址
// System.err.println(cell.getContents());
}
locationList.forEach(row -> {
exec.execute(new Location(row));
});
}
static class Location implements Runnable {
public String contents;
public Location(String contents) {
this.contents = contents;
}
@Override
public void run() {
BaiduApi.getLngAndLat(contents);
}
}
public static void main(String[] args) {
File file = new File("C:\\Users\\admin\\Desktop\\翼联科技文件\\项目文件\\6666.xls");
try {
System.out.println("正在读取书名...");
//取出文件地址
readColumn(file, 9);//读取第一列
System.out.println("读取完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
}