/** * 测试网络状态 * @param transOrgan * @return */ private int testNetState(PetitionOrganTransInfo transOrgan) { int state = 200; try { //先对转发区域网络情况进行检测 String ip = transOrgan.getIpAddress(); String port = transOrgan.getPort(); String address = "http://" + ip; if (port != null && !port.equals("")) { address += ":" + port; } address += "/jubao"; URL url = new URL(address); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(10000); con.setReadTimeout(10000); state = con.getResponseCode(); } catch (Exception e) { //通过报错来判断网络是否联通 state = 0; } return state; }
通过调用上面方法判断网络状态
int state = testNetState(transOrgan); if (state != 200) { System.out.println(transOrgan.getOrgCode()+"网络不通"); state = testNetState(transOrgan); if (state != 200) { System.out.println(transOrgan.getOrgCode()+"网络不通"); continue; } }