提交 506d1fab 编写于 作者: doc_wei's avatar doc_wei

新增区域划分

上级 559ffb2d
......@@ -17,6 +17,11 @@
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 时间操作组件 -->
<dependency>
<groupId>joda-time</groupId>
......
package com.skyeye.common.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class AreaUtil {
/** 获取地区api */
private static final String URL_JD_AREA = "https://d.jd.com/area/get?fid=%d";
/** 初始化省份数据 */
private static final String[] TABLE_PROVINCE = new String[] { "1", "北京", "2", "上海", "3", "天津", "4", "重庆", "5", "河北",
"6", "山西", "7", "河南", "8", "辽宁", "9", "吉林", "10", "黑龙江", "11", "内蒙古", "12", "江苏", "13", "山东", "14", "安徽",
"15", "浙江", "16", "福建", "17", "湖北", "18", "湖南", "19", "广东", "20", "广西", "21", "江西", "22", "四川", "23", "海南",
"24", "贵州", "25", "云南", "26", "西藏", "27", "陕西", "28", "甘肃", "29", "青海", "30", "宁夏", "31", "新疆", "32", "台湾",
"42", "香港", "43", "澳门", "84", "钓鱼岛" };
/**
* 初始化省份数据
*
* @param conn
*/
public static void initArea() {
try {
Connection conn = getConn("127.0.0.1", "3306", "eve", "root", "123456");
for (int nIndex = 0; nIndex < TABLE_PROVINCE.length; nIndex = nIndex + 2) {
int id = Integer.parseInt(TABLE_PROVINCE[nIndex]);
String name = TABLE_PROVINCE[nIndex + 1];
try {
Statement stat = conn.createStatement();
String sql = "INSERT INTO t_area VALUES ('" + ToolUtil.getSurFaceId() + "'," + id + ", '" + name + "', 0, 0)";
stat.execute(sql);
stat.close();
System.out.println("查询:" + name + "--级别:0");
initChildArea(conn, id, 1);
} catch (SQLException e) {
e.printStackTrace();
}
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取各省下级地区
*
* @param conn 数据库连接对象
* @param parentId 所属地区ID
* @param level 地区层级,省级:0,市级:1,...
*/
public static void initChildArea(Connection conn, int parentId, int level) {
String url = String.format(URL_JD_AREA, parentId);
String text = HttpClient.doGet(url);
if (!StringUtils.isEmpty(text)) {
JSONArray array = JSON.parseArray(text);
if (array != null && array.size() > 0) {
for (int nIndex = 0; nIndex < array.size(); nIndex++) {
JSONObject object = array.getJSONObject(nIndex);
int id = object.getInteger("id");
String name = object.getString("name");
try {
Statement stat = conn.createStatement();
String sql = "INSERT INTO t_area VALUES ('" + ToolUtil.getSurFaceId() + "'," + id + ", '" + name + "'," + parentId + ", " + level + ")";
stat.execute(sql);
stat.close();
System.out.println("查询:" + name + "--级别:" + level);
initChildArea(conn, id, level + 1);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 链接数据库
*
* @param dbHost 数据库主机地址
* @param dbPort 数据库端口
* @param dbName 数据库名称
* @param dbUser 数据库用户名称
* @param dbPassword 数据库登录密码
* @return
* @throws Exception
*/
public static Connection getConn(String dbHost, String dbPort, String dbName, String dbUser, String dbPassword) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connStr = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + "?user=" + dbUser + "&password=" + dbPassword + "&characterEncoding=utf8";
Connection conn = DriverManager.getConnection(connStr);
return conn;
}
public static void main(String[] args) {
initArea();
}
}
package com.skyeye.common.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpClient {
public static String doGet(String httpurl) {
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
try {
// 创建远程url连接对象
URL url = new URL(httpurl);
// 通过远程url连接对象打开一个连接,强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式:get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间:60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接,获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is,并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
return result;
}
public static String doPost(String httpUrl, String param) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
URL url = new URL(httpUrl);
// 通过远程url连接对象打开连接
connection = (HttpURLConnection) url.openConnection();
// 设置连接请求方式
connection.setRequestMethod("POST");
// 设置连接主机服务器超时时间:15000毫秒
connection.setConnectTimeout(15000);
// 设置读取主机服务器返回数据超时时间:60000毫秒
connection.setReadTimeout(60000);
// 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
connection.setDoOutput(true);
// 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
connection.setDoInput(true);
// 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
// 通过连接对象获取一个输出流
os = connection.getOutputStream();
// 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
os.write(param.getBytes());
// 通过连接对象获取一个输入流,向远程读取
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 对输入流对象进行包装:charset根据工作项目组的要求来设置
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
// 循环遍历一行一行读取数据
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 断开与远程地址url的连接
connection.disconnect();
}
return result;
}
}
......@@ -2,10 +2,8 @@ package com.skyeye.smprogram.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.skyeye.common.object.InputObject;
......@@ -14,8 +12,6 @@ import com.skyeye.common.util.ToolUtil;
import com.skyeye.smprogram.dao.SmProjectDao;
import com.skyeye.smprogram.service.SmProjectService;
import net.sf.json.JSONArray;
@Service
public class SmProjectServiceImpl implements SmProjectService{
......
......@@ -10,7 +10,6 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.skyeye.common.constans.Constants;
import com.skyeye.common.object.ObjectConstant;
import com.skyeye.common.object.OutputObject;
import com.skyeye.common.object.PutObject;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册