package com.smartearth.poiexcel.controller;
|
|
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.ExcelWriter;
|
import com.alibaba.excel.write.metadata.WriteSheet;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.smartearth.poiexcel.entity.OrgPoiExcel;
|
import com.smartearth.poiexcel.utils.HttpUtils;
|
import com.smartearth.poiexcel.utils.Result;
|
import io.swagger.annotations.Api;
|
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiParam;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.util.StringUtils;
|
import org.springframework.web.bind.annotation.*;
|
import org.apache.commons.io.FileUtils;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.ServletOutputStream;
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
import java.util.*;
|
|
@Controller
|
@RequestMapping(path = "/api/poi/")
|
@Api(tags = {"POI相关接口"})
|
@Slf4j
|
public class OrgPoiController {
|
|
//北京数据平台poi接口转发
|
@GetMapping(path = "/queryBJGdPoi")
|
@ApiOperation(value = "查询GdPoi(北京数据平台)", notes = "多个关键字用“|”分割", hidden = true)
|
public void queryBJGdPoi(
|
@ApiParam(name = "keywords", value = "查询关键字", defaultValue = "数字北京大厦", required = true)
|
@RequestParam(name = "keywords") String keywords,
|
@ApiParam(name = "output", value = "返回数据格式(默认json,可选json/xml)", defaultValue = "json", required = true)
|
@RequestParam(name = "output") String output,
|
@ApiParam(name = "offset", value = "每页记录数据,默认20,不超过25", defaultValue = "20")
|
@RequestParam(name = "offset") String offset,
|
@ApiParam(name = "page", value = "当前页数,默认1,最大100", defaultValue = "1")
|
@RequestParam(name = "page") String page,
|
@ApiParam(name = "coord", value = "bjl地方坐标,cgcs2000国家2000坐标", defaultValue = "cgcs2000", required = true)
|
@RequestParam(name = "coord") String coord,
|
HttpServletResponse response
|
) throws IOException {
|
try {
|
//请求url
|
String url = "http://172.26.64.84/service/NavigationService";
|
//设置请求参数
|
Map<String, String> params = new HashMap<>();
|
params.put("request", "GdPoi");
|
params.put("keywords", StringUtils.isEmpty(keywords) ? "" : keywords);
|
params.put("output", StringUtils.isEmpty(output) ? "json" : output);
|
params.put("offset", StringUtils.isEmpty(offset) ? "20" : offset);
|
params.put("page", StringUtils.isEmpty(page) ? "1" : page);
|
params.put("coord", StringUtils.isEmpty(coord) ? "cgcs2000" : coord);
|
//设置请求头
|
Map<String, String> headers = new HashMap<>();
|
headers.put("Accept", "*/*");
|
//Base64加密Authorization认证下的basic auth 用户名:密码
|
// headers.put("Authorization","Basic ampqc2tmcTpKampza2ZxQDIwMjI=");
|
headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
|
//执行请求
|
String responseString = HttpUtils.get(url, params, headers);
|
// return Result.ok(responseString);
|
//返回值在response响应中
|
response.setHeader("Content-Type", "application/json;charset=UTF-8");
|
response.getWriter().write(responseString);
|
} catch (Exception e) {
|
e.printStackTrace();
|
// return Result.error("未知错误");
|
response.getWriter().write("未知错误");
|
}
|
|
}
|
|
@GetMapping(path = "/queryBJbdPoi")
|
@ApiOperation(value = "查询bdPoi(北京数据平台)", notes = "查询不支持多关键字检索,可查类型,如query=美食", hidden = true)
|
public void queryBJbdPoi(
|
@ApiParam(name = "query", value = "查询关键字", defaultValue = "数字北京大厦", required = true)
|
@RequestParam(name = "query") String query,
|
@ApiParam(name = "output", value = "返回数据格式(默认json,可选json/xml)", defaultValue = "json", required = true)
|
@RequestParam(name = "output") String output,
|
@ApiParam(name = "page_num", value = "分页页码,默认为0,0代表第一页", defaultValue = "0")
|
@RequestParam(name = "page_num") String page_num,
|
@ApiParam(name = "page_size", value = "单次POI数量,默认为10条,最大20条", defaultValue = "10")
|
@RequestParam(name = "page_size") String page_size,
|
@ApiParam(name = "coord", value = "bjl地方坐标,cgcs2000国家2000坐标", defaultValue = "cgcs2000", required = true)
|
@RequestParam(name = "coord") String coord,
|
HttpServletResponse response
|
) throws IOException {
|
try {
|
//请求url
|
String url = "http://172.26.64.84/service/NavigationService";
|
//设置请求参数
|
Map<String, String> params = new HashMap<>();
|
params.put("request", "bdPoi");
|
params.put("query", StringUtils.isEmpty(query) ? "" : query);
|
params.put("output", StringUtils.isEmpty(output) ? "json" : output);
|
params.put("page_num", StringUtils.isEmpty(page_num) ? "0" : page_num);
|
params.put("page_size", StringUtils.isEmpty(page_size) ? "10" : page_size);
|
params.put("coord", StringUtils.isEmpty(coord) ? "cgcs2000" : coord);
|
//设置请求头
|
Map<String, String> headers = new HashMap<>();
|
headers.put("Accept", "*/*");
|
//Base64加密Authorization认证下的basic auth 用户名:密码
|
headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
|
//执行请求
|
String responseString = HttpUtils.get(url, params, headers);
|
// return Result.ok(responseString);
|
//返回值在response响应中
|
response.setHeader("Content-Type", "application/json;charset=UTF-8");
|
response.getWriter().write(responseString);
|
} catch (Exception e) {
|
e.printStackTrace();
|
// return Result.error("未知错误");
|
response.getWriter().write("未知错误");
|
}
|
|
}
|
|
@GetMapping(path = "/queryBJhint")
|
@ApiOperation(value = "输入提示(北京数据平台)", notes = "POI分类,多个类型间用“|”分隔", hidden = true)
|
public void queryBJhint(
|
@ApiParam(name = "keywords", value = "查询关键字", defaultValue = "数字北京大厦", required = true)
|
@RequestParam(name = "keywords") String keywords,
|
@ApiParam(name = "output", value = "返回数据格式(默认json,可选json/xml)", defaultValue = "json", required = true)
|
@RequestParam(name = "output") String output,
|
// @ApiParam(name = "type", value = "POI分类,多个类型间用“|”分隔,可选值:POI分类名称")
|
// @RequestParam(name = "type",required = false) String type,
|
@ApiParam(name = "datatype",
|
value = "返回的数据类型,多种数据类型用“|”分隔," +
|
"可选值:all-返回所有数据类型、poi-返回POI数据类型、" +
|
"bus-返回公交站点数据类型、busline-返回公交线路数据类型", defaultValue = "all")
|
@RequestParam(name = "datatype") String datatype,
|
@ApiParam(name = "coord", value = "bjl地方坐标,cgcs2000国家2000坐标", defaultValue = "cgcs2000", required = true)
|
@RequestParam(name = "coord") String coord,
|
HttpServletResponse response
|
) throws IOException {
|
try {
|
//请求url
|
String url = "http://172.26.64.84/service/NavigationService?request=hint&type=&";
|
//设置请求参数
|
Map<String, String> params = new HashMap<>();
|
params.put("keywords", StringUtils.isEmpty(keywords) ? "" : keywords);
|
params.put("output", StringUtils.isEmpty(output) ? "json" : output);
|
// params.put("type",StringUtils.isEmpty(type)?"":type);
|
params.put("datatype", StringUtils.isEmpty(datatype) ? "all" : datatype);
|
params.put("coord", StringUtils.isEmpty(coord) ? "cgcs2000" : coord);
|
//设置请求头
|
Map<String, String> headers = new HashMap<>();
|
headers.put("Accept", "*/*");
|
//Base64加密Authorization认证下的basic auth 用户名:密码
|
headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
|
//执行请求
|
String responseString = HttpUtils.get(url, params, headers);
|
// return Result.ok(responseString);
|
//返回值在response响应中
|
response.setHeader("Content-Type", "application/json;charset=UTF-8");
|
response.getWriter().write(responseString);
|
} catch (Exception e) {
|
e.printStackTrace();
|
// return Result.error("未知错误");
|
response.getWriter().write("未知错误");
|
}
|
|
}
|
|
@Value("${excel.export.dir}")
|
private String excelExportDir;
|
|
@Value("${server.port}")
|
private int serverPort;
|
|
@ApiOperation(value = "从Excel导入导出",
|
notes = "<p>将导入文件中待转换处理的地址表头修改为\"待转换地址\"(或者增加此列)</p>" +
|
"<p>文件导出至D:\\excel\\ExcelWrite.xlsx</p>")
|
@RequestMapping(value = "/importBatchExcel", method = RequestMethod.POST)
|
@ResponseBody
|
public Result importBatchExcel(HttpServletRequest request, @RequestPart("multipartFile") MultipartFile multipartFile) {
|
ExcelWriter excelWriter = null;
|
int k = 0;
|
int j = 0;
|
try {
|
// String tmp = System.getProperty("java.io.tmpdir");
|
// if (!tmp.endsWith(File.separator)) {
|
// tmp += File.separator;
|
// }
|
File file = new File(excelExportDir + "ExcelRead.xlsx");
|
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
|
//默认读取Excel文件的第一个sheet
|
List<OrgPoiExcel> list = EasyExcel.read(file).head(OrgPoiExcel.class).sheet().doReadSync();
|
List<OrgPoiExcel> listWrite = new ArrayList<>();
|
|
File fileWrite = new File(excelExportDir + "ExcelWrite.xlsx");
|
// EasyExcel.write(fileWrite, OrgPoiExcel.class).sheet("Sheet1").doWrite(listWrite);
|
excelWriter = EasyExcel.write(fileWrite, OrgPoiExcel.class).build();
|
WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
|
|
for (int i = 0; i < list.size(); i++) {//i=60653
|
OrgPoiExcel data = list.get(i);
|
// String businessName = data.getBusinessName();
|
String address = data.getAddress();
|
String resultString = selectAddrCode(address);
|
JSONObject jsonObject = JSONObject.parseObject(resultString);
|
if (jsonObject != null && jsonObject.get("status").equals("1")) {
|
JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
|
if (jsonArray != null && jsonArray.size() > 0) {
|
JSONObject object = jsonArray.getJSONObject(0);
|
// String businessNameJson = object.getString("name");
|
String formatted_address = object.getString("formatted_address");
|
if (!StringUtils.isEmpty(formatted_address)) {
|
String encoding = getEncoding(formatted_address);
|
//log.info("encoding: {}", encoding);
|
if (!"UTF-8".equals(encoding) && !"".equals(encoding)) {
|
formatted_address = new String(formatted_address.getBytes(encoding), "UTF-8");
|
}
|
data.setStandardAddress(formatted_address);
|
} else {
|
data.setStandardAddress(null);
|
}
|
String location = object.getString("location");
|
String[] split = location.split(",");
|
data.setX(Double.parseDouble(split[1]));
|
data.setY(Double.parseDouble(split[0]));
|
} else {
|
data.setX(0.0);
|
data.setY(0.0);
|
}
|
} else {
|
data.setX(0.0);
|
data.setY(0.0);
|
j++;
|
if (j == 3000) {
|
excelWriter.finish();
|
return Result.error("返回值为0超过3000次");
|
}
|
}
|
k++;
|
System.out.println(k + ":" + data);
|
listWrite.add(data);
|
excelWriter.write(listWrite, writeSheet);
|
listWrite.remove(0);
|
}
|
// EasyExcel.write(fileWrite, OrgPoiExcel.class).sheet("Sheet1").doWrite(listWrite);
|
if (excelWriter != null) {
|
excelWriter.finish();
|
}
|
StringBuilder url = new StringBuilder("http://");
|
url.append(request.getLocalAddr()).append(":")
|
.append(request.getLocalPort())
|
.append("/api/poi/downloadFile");
|
return Result.ok("下载文件,请在浏览其中打开地址 " + url.toString());
|
} catch (Exception e) {
|
e.printStackTrace();
|
if (excelWriter != null) {
|
excelWriter.finish();
|
}
|
return Result.error(e.toString());
|
}
|
}
|
|
@GetMapping(value = "downloadFile")
|
@ApiOperation(value = "下载文件", hidden = true)
|
public void downloadFile(HttpServletResponse response) {
|
File fileWrite = new File(excelExportDir + "ExcelWrite.xlsx");
|
String fileName = "excel.xlsx";
|
response.reset();
|
response.setContentType("application/vnd.ms-excel;charset=utf-8");
|
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
|
FileInputStream fis = null;
|
try {
|
fis = new FileInputStream(fileWrite);
|
int len;
|
byte[] bytes = new byte[1024];
|
ServletOutputStream outputStream = response.getOutputStream();
|
while ((len = fis.read(bytes)) > 0) {
|
outputStream.write(bytes, 0, len);
|
}
|
fis.close();
|
outputStream.close();
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
private String selectBJGdPoi(
|
String keywords) throws Exception {
|
StringBuilder url = new StringBuilder(
|
"http://172.26.64.84/service/NavigationService?request=GdPoi");
|
//设置请求参数
|
if (!StringUtils.isEmpty(keywords)) {
|
url.append("&keywords=").append(keywords).append("&output=json&offset=20&page=1&coord=cgcs2000");
|
}
|
//设置请求头
|
Map<String, String> headers = new HashMap<>();
|
// headers.put("Authorization","Basic ampqc2tmcTpKampza2ZxQDIwMjI=");
|
headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
|
//执行请求
|
return HttpUtils.get(url.toString(), null, headers);
|
}
|
|
@Value("${address.code.url}")
|
private String addressCodeUrl;
|
|
private String selectAddrCode(
|
String address) throws Exception {
|
StringBuilder url = new StringBuilder(addressCodeUrl).append(
|
"?address=");
|
//设置请求参数
|
if (!StringUtils.isEmpty(address)) {
|
url.append(address).append("&output=json&batch=true&coord=cgcs2000&adcode=yes");
|
}
|
//设置请求头
|
Map<String, String> headers = new HashMap<>();
|
headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
|
|
//执行请求
|
return HttpUtils.get(url.toString(), null, headers);
|
}
|
|
public static String getEncoding(String str) {
|
String encode = "GB2312";
|
try {
|
if (isEncoding(str, encode)) { // 判断是不是GB2312
|
return encode;
|
}
|
} catch (Exception exception) {
|
}
|
encode = "ISO-8859-1";
|
try {
|
if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1
|
return encode;
|
}
|
} catch (Exception exception1) {
|
}
|
encode = "UTF-8";
|
try {
|
if (isEncoding(str, encode)) { // 判断是不是UTF-8
|
return encode;
|
}
|
} catch (Exception exception2) {
|
}
|
encode = "GBK";
|
try {
|
if (isEncoding(str, encode)) { // 判断是不是GBK
|
return encode;
|
}
|
} catch (Exception exception3) {
|
}
|
return ""; // 如果都不是,说明输入的内容不属于常见的编码格式。
|
}
|
|
public static boolean isEncoding(String str, String encode) {
|
try {
|
if (str.equals(new String(str.getBytes(), encode))) {
|
return true;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
}
|