Java tutorial
/* * Copyright 2015-9999 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 com.beginner.core.utils; import java.io.File; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; import java.util.Set; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.ObjectName; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * <b>??</b>PublicUtil<br/> * <b>??</b><br/> * <b></b>Hsiao Lin Studio<br/> * <b></b><br/> * <b></b>20150521 ?6:18:18<br/> * <b></b><br/> * @version 1.0.0<br/> */ public class ProjectUtil { private static Logger logger = LoggerFactory.getLogger(ProjectUtil.class); /** * ? * @return String D:\workspace\Beginner\ * @since 1.0.0 */ public static String getProjectPath() { return System.getProperty("user.dir") + "\\"; } /** * ?Host */ public static String remoteHost() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); return request.getRemoteHost(); } /** * ?IP? */ public static String remoteAddr() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); return request.getRemoteAddr(); } /** * ?IP(?????) */ public static String ip() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); return request.getLocalAddr(); } /** * ?IP(???) */ public static String getIp() { InetAddress addr = null; String ip; try { addr = InetAddress.getLocalHost(); } catch (UnknownHostException e) { logger.error("??IP??", e); } ip = addr.getHostAddress().toString(); return ip; } /** * ?Web???(?????) */ public static int port() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); return request.getLocalPort(); } /** * ?Web???(???) */ public static String getPort() { String result = ""; try { MBeanServer mBeanServer = null; List<MBeanServer> mBeanServers = MBeanServerFactory.findMBeanServer(null); if (mBeanServers.size() > 0) { for (MBeanServer _mBeanServer : mBeanServers) { mBeanServer = _mBeanServer; break; } } if (mBeanServer == null) { throw new IllegalStateException("?JVM?MBeanServer."); } Set<ObjectName> objectNames = null; objectNames = mBeanServer.queryNames(new ObjectName("Catalina:type=Connector,*"), null); if (objectNames == null || objectNames.size() <= 0) { throw new IllegalStateException("?JVM?MBeanServer : " + mBeanServer.getDefaultDomain() + " ??."); } for (ObjectName objectName : objectNames) { String protocol = (String) mBeanServer.getAttribute(objectName, "protocol"); if (protocol.equals("HTTP/1.1")) { int port = (Integer) mBeanServer.getAttribute(objectName, "port"); result = port + StringUtils.EMPTY; } } } catch (Exception e) { logger.error("?WEB?HTTP/1.1???", e); } return result; } /** * * * @param pathType visit-save-? * @param pathCategory ?-topic???-reply? * @return String */ public static String getPicturePath(String pathType, String pathCategory) { String strResult = ""; StringBuffer strBuf = new StringBuffer(); if ("visit".equals(pathType)) { } else if ("save".equals(pathType)) { String projectPath = getProjectPath().replaceAll("\\\\", "/"); projectPath = splitString(projectPath, "bin/"); strBuf.append(projectPath); strBuf.append("webapps/ROOT/"); } strResult = strBuf.toString(); return strResult; } private static String splitString(String str, String param) { String result = str; if (str.contains(param)) { int start = str.indexOf(param); result = str.substring(0, start); } return result; } /** * ?classpath * @return String D:/workspace/Beginner/src/main/webapp/WEB-INF/classes/../../ */ public static String getClasspath() { String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "../../") .replaceAll("file:/", "").replaceAll("%20", " ").trim(); if (path.indexOf(":") != 1) { path = File.separator + path; } return path; } /** * ?classpath * @return String D:/workspace/Beginner/src/main/webapp/WEB-INF/classes/ */ public static String getClassResources() { String path = (String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))) .replaceAll("file:/", "").replaceAll("%20", " ").trim(); if (path.indexOf(":") != 1) { path = File.separator + path; } return path; } /** * ???(?????) */ public static String path() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); return request.getContextPath(); } /** * ???(??) */ public static String appName() { String appName = System.getProperty("user.dir"); return appName.substring(appName.lastIndexOf("\\") + 1, appName.length()); } /** * ? * @return String http://ip?:??/??/ */ public static String basePath() { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()) .getRequest(); String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; return basePath; } }