java web判斷服務器是不是本機
1,如何獲取瀏覽器的ip web
Java代碼
- /***
- * 獲取客戶端ip地址(能夠穿透代理)
- * @param request
- * @return
- */
- public static String getClientIpAddr(HttpServletRequest request) {
- String ip = request.getHeader("X-Forwarded-For");
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_X_FORWARDED_FOR");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_X_FORWARDED");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_X_CLUSTER_CLIENT_IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_CLIENT_IP");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_FORWARDED_FOR");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_FORWARDED");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("HTTP_VIA");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getHeader("REMOTE_ADDR");
- }
- if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
- ip = request.getRemoteAddr();
- }
- return ip;
- }
- public static String getIpAddr(HttpServletRequest request) {
- String ip = request.getHeader("X-Real-IP");
- if (null != ip && !"".equals(ip.trim())
- && !"unknown".equalsIgnoreCase(ip)) {
- return ip;
- }
- ip = request.getHeader("X-Forwarded-For");
- if (null != ip && !"".equals(ip.trim())
- && !"unknown".equalsIgnoreCase(ip)) {
- // get first ip from proxy ip
- int index = ip.indexOf(',');
- if (index != -1) {
- return ip.substring(0, index);
- } else {
- return ip;
- }
- }
- return request.getRemoteAddr();
- }
2,如何判斷服務器是不是本機 apache
Java代碼
- /***
- * 服務器是不是本機
- * @param request
- * @return
- */
- public static boolean isLocalIp(HttpServletRequest request){
- String ip=WebServletUtil.getClientIpAddr(request);
- return ip.equals("127.0.0.1")||ip.equals("localhost")||ip.equals("0:0:0:0:0:0:0:1");
- }
3,應用: 瀏覽器
Java代碼
- /***
- * favicon.ico
- * @throws IOException
- */
- @RequestMapping(value = "/favicon.ico")
- public ResponseEntity<byte[]> faviconIco(HttpServletRequest request) throws IOException {
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.IMAGE_PNG);
- String faviconIcoName="sms-4.ico";
- headers.set(Constant2.CONTENT_DISPOSITION,WebServletUtil.getContentDisposition(true, faviconIcoName));
- ///home/whuang/software/apache-tomcat-7.0.53/webapps/ROOT/
- String webappPath=null;
- if(WebServletUtil.isLocalIp(request)){//服務器在本機(訪問ip爲127或localhost)
- webappPath=WebServletUtil.getRealPath(request);
- }else{
- webappPath=DictionaryParam.get(Constant2.DICTIONARY_GROUP_GLOBAL_SETTING, "WEB-INF_LOC");
- }
- return new ResponseEntity<byte[]>(FileUtils.getBytes4File(
- webappPath
- +"WEB-INF/static/img/"+faviconIcoName),
- headers, HttpStatus.CREATED);
-
- }
4,如何獲取服務器部署的本地路徑 tomcat
Java代碼
- public static String getRealPath(HttpServletRequest request) {
- return getRealPath(request, "\\");
- }
- /**
- * 獲取相對地址的絕對地址
- *
- * @param request
- * @param relativePath
- * @return
- */
- public static String getRealPath(HttpServletRequest request, String relativePath) {
- return request.getSession().getServletContext().getRealPath(relativePath);
- }
歡迎關注本站公眾號,獲取更多信息