diff --git a/skyeye-common/pom.xml b/skyeye-common/pom.xml index e1760bb647f0f485e910ae2d7675e709a70b52d3..085a7942a1ef4e2bef10033c2bfed880318e2996 100644 --- a/skyeye-common/pom.xml +++ b/skyeye-common/pom.xml @@ -17,6 +17,11 @@ org.springframework spring-webmvc + + + mysql + mysql-connector-java + joda-time diff --git a/skyeye-common/src/main/java/com/skyeye/common/util/AreaUtil.java b/skyeye-common/src/main/java/com/skyeye/common/util/AreaUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..33fd36668593447cb86d685dd7b079a712e7b14e --- /dev/null +++ b/skyeye-common/src/main/java/com/skyeye/common/util/AreaUtil.java @@ -0,0 +1,109 @@ +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(); + } + +} diff --git a/skyeye-common/src/main/java/com/skyeye/common/util/HttpClient.java b/skyeye-common/src/main/java/com/skyeye/common/util/HttpClient.java new file mode 100644 index 0000000000000000000000000000000000000000..62dc9bade5312d8c61a120b8f5dbd7c78aa92a79 --- /dev/null +++ b/skyeye-common/src/main/java/com/skyeye/common/util/HttpClient.java @@ -0,0 +1,144 @@ +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; + } +} diff --git a/skyeye-service/src/main/java/com/skyeye/smprogram/service/impl/SmProjectServiceImpl.java b/skyeye-service/src/main/java/com/skyeye/smprogram/service/impl/SmProjectServiceImpl.java index 1aaece2f9a89ea9d2c10c3d3750aeabc0e2ee7e6..bb05293b7787501991e84440cd9ec3a0182018aa 100644 --- a/skyeye-service/src/main/java/com/skyeye/smprogram/service/impl/SmProjectServiceImpl.java +++ b/skyeye-service/src/main/java/com/skyeye/smprogram/service/impl/SmProjectServiceImpl.java @@ -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{ diff --git a/skyeye-web/src/main/java/com/skyeye/common/interceptor/HandlerInterceptorMain.java b/skyeye-web/src/main/java/com/skyeye/common/interceptor/HandlerInterceptorMain.java index 8ed16e9741b6a878bcb28edd6413a1cf1c622d78..d6c5ac093edfe5eb1b3156f555e8271e2f40f1fc 100644 --- a/skyeye-web/src/main/java/com/skyeye/common/interceptor/HandlerInterceptorMain.java +++ b/skyeye-web/src/main/java/com/skyeye/common/interceptor/HandlerInterceptorMain.java @@ -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;