/* * Copyright 2008-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package kcc.com.cmm.util; import java.io.UnsupportedEncodingException; import java.net.InetAddress; import java.net.URLEncoder; import java.net.UnknownHostException; import javax.servlet.http.HttpServletRequest; public class IpUtil { public static String getClientIP(HttpServletRequest request) { String userip = request.getHeader("X-Forwarded-For"); // 아이피 가져오기 아파치 아래에 웹로직이 있을경우 if ( userip == null || "".equals(userip) ) { // 아이피 가져오기 , 바로 웹로직이 있을경우 userip = request.getRemoteAddr(); } if ( userip == null || "".equals(userip) ) { return ""; } String[] userips = userip.split(","); return userips[0]; } public static String getBrowser(HttpServletRequest request) { String header = request.getHeader("User-Agent"); //System.out.println("header:"+header); if (header.indexOf("MSIE") > -1 || header.indexOf("rv:11.0") > -1) { return "MSIE"; }else if (header.indexOf("Opera") > -1 || header.indexOf("OPR") > -1) { return "Opera"; }else if (header.indexOf("Chrome") > -1) { return "Chrome"; }else if (header.indexOf("Firefox") > -1) { return "Firefox"; } return "Firefox"; } public static String getStr(HttpServletRequest request,String str) throws UnsupportedEncodingException { String header = request.getHeader("User-Agent"); String browser; if (header.indexOf("MSIE") > -1 || header.indexOf("rv:11.0") > -1) { browser = "MSIE"; } else if (header.indexOf("Chrome") > -1) { browser = "Chrome"; } else if (header.indexOf("Opera") > -1) { browser = "Opera"; } else { browser = "Firefox"; } if (browser.equals("MSIE")) { str = URLEncoder.encode(str, "UTF-8") .replaceAll("\\+", "%20"); } else if (browser.equals("Firefox")) { str = new String(str.getBytes("UTF-8"), "8859_1"); } else if (browser.equals("Opera")) { str = new String(str.getBytes("UTF-8"), "8859_1"); } else if (browser.equals("Chrome")) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (c > '~') { sb.append(URLEncoder.encode("" + c, "UTF-8")); } else { sb.append(c); } } str = sb.toString(); } else { str = "Not supported browser"; } return str; } public static String getOzServerName(String serverNm) { String ozServerNm = ""; if(serverNm.contains("localhost") || serverNm.contains("119.193.215.98") || serverNm.contains("iten.co.kr") || serverNm.contains("192.168.0.176") || serverNm.contains("ljhtest")) {//로컬 및 개발서버 요청시 서버 경로 처리 ozServerNm = "http://119.193.215.98:8086"; // ozServerNm = "http://192.168.0.176:8091"; }else if(serverNm.contains("192.168.39.144")) {//저작위 업무시스템망에서 오즈 요청시 오즈 서버 경로 처리 ozServerNm = "http://192.168.39.145:8080"; } return ozServerNm; } public static String getClientIPv4(HttpServletRequest request) { String userip = request.getHeader("X-Forwarded-For"); // 아파치 아래에 웹로직이 있을경우 아이피 가져오기 if (userip == null || "".equals(userip)) { // 아이피 가져오기 , 바로 웹로직이 있을경우 userip = request.getRemoteAddr(); } if (userip == null || "".equals(userip)) { return ""; } // 여러 IP가 있을 경우 첫 번째 IP 사용 String[] userips = userip.split(","); userip = userips[0].trim(); // IPv6 주소인 경우 IPv4로 변환 시도 try { InetAddress inetAddress = InetAddress.getByName(userip); if (inetAddress instanceof java.net.Inet6Address) { byte[] ipv4Bytes = new byte[4]; System.arraycopy(inetAddress.getAddress(), 12, ipv4Bytes, 0, 4); InetAddress ipv4Address = InetAddress.getByAddress(ipv4Bytes); return ipv4Address.getHostAddress(); } } catch (UnknownHostException e) { e.printStackTrace(); // 예외 발생 시 로그 출력 } // IPv4 주소라면 그대로 반환 return userip; } }