提交 39b83f90 编写于 作者: Z zhipeng.zhang

优化经纬度查询

上级 3553c412
......@@ -186,6 +186,11 @@
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
</dependencies>
<build>
<finalName>picture-bed</finalName>
......
package com.tools.common.excel;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;
/**
* Excel 工具类
*/
public class ExcelUtil {
/**
* 导出 Excel :一个 sheet,带表头.
* @param response HttpServletResponse
* @param data 数据 list,每个元素为一个 BaseRowModel
* @param fileName 导出的文件名
* @param sheetName 导入文件的 sheet 名
* @param model 映射实体类,Excel 模型
* @throws Exception 异常
*/
public static void writeExcel(HttpServletResponse response, List<? extends Object> data,
String fileName, String sheetName, Class model) throws Exception {
/**
* 头的策略 设置表头居中对齐
*/
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
/**
* 内容的策略 设置内容靠左对齐
*/
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
EasyExcel.write(getOutputStream(fileName, response), model)
.excelType(ExcelTypeEnum.XLSX)
.sheet(sheetName)
.registerWriteHandler(horizontalCellStyleStrategy)
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
.doWrite(data);
}
/**
* 导出文件时为Writer生成OutputStream.
* @param fileName 文件名
* @param response response
* @return ""
*/
private static OutputStream getOutputStream(String fileName,
HttpServletResponse response) throws Exception {
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
response.setHeader("Pragma", "public");
response.setHeader("Cache-Control", "no-store");
response.addHeader("Cache-Control", "max-age=0");
return response.getOutputStream();
} catch (IOException e) {
throw new Exception("导出excel表格失败!", e);
}
}
}
\ No newline at end of file
package com.tools.common.excel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.BasicParameter;
import lombok.Data;
import java.io.Serializable;
@Data
@ColumnWidth(22)
@ContentRowHeight(15)
public class User implements Serializable {
/**
* value: 表头名称
* index: 列的号, 0表示第一列
*
*/
/**
* 入库时间
*/
@ExcelProperty(value = "姓名", index = 0)
private String name;
/**
* 供应商
*/
@ExcelProperty(value = "年龄", index = 1)
private String age;
/**
* 纸筒编号
*/
@ExcelProperty(value = "性别", index = 2)
private String sex;
}
......@@ -6,13 +6,11 @@ import com.tools.common.model.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
......@@ -27,9 +25,11 @@ public class BaiDuMapUtils {
static Logger logger = LoggerFactory.getLogger(BaiDuMapUtils.class);
static String AK = "888";
static String AK = "*****************";
static String SN ="888";
static String SN ="*****************";
static String STATUS ="status";
public static void main(String[] args) {
String dom = "北京王府井";
......@@ -44,12 +44,22 @@ public class BaiDuMapUtils {
public static Result getCoordinate(String address) {
if (address != null && !"".equals(address)) {
address = address.replaceAll("\\s*", "").replace("#", "栋");
String url = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + AK+"&SN="+SN;
String json = loadJSON(url);
String url = "http://api.map.baidu.com/geocoding/v3";
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("address",address);
params.add("output","json");
params.add("ak",AK);
params.add("sn",SN);
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
String json = response.getBody();
logger.info("位置信息:{}",json);
if (StringUtils.isNotBlank(json)) {
JSONObject obj = JSONObject.parseObject(json);
if (SystemConstant.CODE_0.equals(obj.getString("status"))) {
if (SystemConstant.CODE_0.equals(obj.getString(STATUS))) {
/**
* 经度
*/
......@@ -73,23 +83,4 @@ public class BaiDuMapUtils {
return Result.error();
}
}
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;
while ((inputLine = in.readLine()) != null) {
json.append(inputLine);
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json.toString();
}
}
......@@ -8,7 +8,6 @@ import com.tools.module.app.entity.AppDingUser;
import com.tools.module.app.service.AppDingService;
import com.tools.module.app.service.AppDingUserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册