主要用于不同网址间数据共同访问;
/**
1、连接网址,并获取返回数据json,记住callback=JSON_CALLBACK一定要加
*/
public String getHttpResponse(String id) { String allConfigUrl ="网址?callback=JSON_CALLBACK&参数=????"; BufferedReader in = null; StringBuffer result = null; try { URI uri = new URI(allConfigUrl); URL url = uri.toURL(); URLConnection connection = url.openConnection(); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Charset", "utf-8"); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)"); connection.connect(); result = new StringBuffer(); //读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line=null; while (!((line=in.readLine())==null)) { result.append(line); } return result.toString(); } catch (Exception e) { e.printStackTrace(); }finally { } return result.toString(); } /** * 调用程序,因为这个在以前的一个系统里面写的,时间紧急就只整了里面没有单独分离出 */ @RequestMapping({ "/test.do" }) public @ResponseBody String test (HttpServletRequest request, HttpServletResponse response) throws Exception { response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); PrintWriter writer = response.getWriter(); JSONObject result = new JSONObject(); String sql = "select * from sys_tree_test where id not in (select cid from sys_tree_his) "; List<Map> list = dbHelper.getRows(sql,null); if (list != null && list.size()>0) { for(Map map:list){ String id = map.get("id").toString(); String parent_ids = map.get("parent_ids").toString(); String id1 = map.get("id1").toString(); Thread.currentThread().sleep(200);//毫秒 System.out.println(id1); String json = getHttpResponse(id1); System.out.println(json); if(json.contains("http-equiv")){ UUID uuid = UUID.randomUUID(); String idS = uuid.toString().replace("-", ""); String selSql ="insert into SYS_TREE_MSG values('"+id+"','加载错误!!!','"+idS+"')"; dbHelper.runSql(sql); break; } getObjects(json,id,parent_ids,id1); } } System.out.println("。。。。运行完毕。。。。"); result.put("success", true); result.put("data", null); writer.write(result.toString()); return null; } public void getObjects(String json,String parent_id,String parent_ids,String id2) throws Exception{ if(!Util.isEoN(json)){ JSONObject jsonObj = JSONObject.fromObject(json); String cubeClsSearchResponse = jsonObj.optString("cubeClsSearchResponse"); String sql = "insert into sys_tree_his(id,id1,cid)values('"+parent_id+"','"+id2+"','"+parent_id+"')"; dbHelper.runSql(sql); if(!Util.isEoN(cubeClsSearchResponse)){ JSONObject resObject= JSONObject.fromObject(cubeClsSearchResponse); String patClsList = resObject.optString("patClsList"); if(!Util.isEoN(patClsList)){ JSONArray jsonArray= JSONArray.fromObject(patClsList); for(Object obj:jsonArray){ JSONObject jsonObject = (JSONObject)obj; saveobjects (jsonObject,parent_id,parent_ids,id2); } } } } } public void saveobjects (JSONObject jsonObject,String parent_id,String parent_ids,String id2) throws Exception{ String id1=jsonObject.optString("id"); String cls=jsonObject.optString("cls"); String des=jsonObject.optString("des"); UUID uuid = UUID.randomUUID(); String id = uuid.toString().replace("-", ""); parent_ids = parent_ids + parent_id + "/"; String selSql = "select * from sys_tree_test where id1='"+id1+"'"; List list = dbHelper.getRows(selSql,null); if(list.size()>0){ return; } String sql = "insert into sys_tree_test(id,code,name,parent_id,parent_ids,id1,id2) values('" +id+"','"+cls+"','"+des+"','"+parent_id+"','"+parent_ids+"','"+id1+"','"+id2+"')"; System.out.println(sql); dbHelper.runSql(sql); } /** * 搜索所有ID */ @RequestMapping({ "/save.do" }) public @ResponseBody String save(@RequestBody String example,HttpServletRequest request, HttpServletResponse response) throws Exception { response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); if(Util.isEoN(example)){ example = "{}"; } JSONObject jsonObject = JSONObject.fromObject(example); String id=jsonObject.optString("id"); String cls=jsonObject.optString("cls"); String des=jsonObject.optString("des"); String parent_id=jsonObject.optString("parent_id"); PrintWriter writer = response.getWriter(); JSONObject result = new JSONObject(); String sql = "inser into sys_tree_test(id,code,name,parent_id) values('"+id+"','"+cls+"','"+des+"','"+parent_id+"');"; System.out.println(sql); dbHelper.runSql(sql); result.put("success", true); writer.write(result.toString()); return null; }