精华内容
下载资源
问答
  • java获取服务器地址

    千次阅读 2010-07-29 18:08:00
    来访者ip ServletActionContext.getRequest().getRemoteHost() 本应用所在服务器地址: InetAddress myIPaddress=InetAddress.getLocalHost();  String mi = myIPaddress....

    来访者ip

    ServletActionContext.getRequest().getRemoteHost()

    本应用所在服务器地址:

    InetAddress myIPaddress=InetAddress.getLocalHost();
                String mi = myIPaddress.getHostAddress();

    本机ip:

                Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();
                while (netInterfaces.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                    InetAddress ip = (InetAddress) ni.getInetAddresses().nextElement();
                    ip.getHostAddress();
                 ///
                }

    展开全文
  • Java中,可以使用InetAddress.getLocalHost()来获取运行Java应用程序的当前服务器的IP地址。 package com.mkyong; import java.net.InetAddress; import java.net.UnknownHostException; public class Test { ...

    在Java中,可以使用InetAddress.getLocalHost()来获取运行Java应用程序的当前服务器的IP地址。

    package com.mkyong;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    
    public class Test {
    
    	public static void main(String[] args) {
    
    	  InetAddress ip;
    	  try {
    
    		ip = InetAddress.getLocalHost();
    		System.out.println("Current IP address : " + ip.getHostAddress());
    
    	  } catch (UnknownHostException e) {
    
    		e.e.printStackTrace();
    
    	  }
    
    	}
    
    }

    输出量

    Current IP address : 192.168.1.22

    参考文献

    1. InetAddress Java文档
    2. 如何在Java中获取客户端IP地址

    翻译自: https://mkyong.com/java/how-to-get-ip-address-in-java/

    展开全文
  • java获取服务器信息

    千次阅读 2017-01-16 10:42:51
    java获取服务器信息需要用到sigar.jar,但该jar在linux下依赖于libsigar-amd64-linux.so、libsigar-x86-linux.so。在window下依赖于sigar-amd64-winnt.dll、sigar-x86-winnt.dll。配置文件这存在这模拟系统命名的...

    java获取服务器信息需要用到sigar.jar,但该jar在linux下依赖于libsigar-amd64-linux.so、libsigar-x86-linux.so。在window下依赖于sigar-amd64-winnt.dll、sigar-x86-winnt.dll。配置文件这存在这模拟系统命名的操作,可以通过这些系统命名来获取系统的参数。


    默认情况下是将依赖文件放在jdk下的lib包中,这样项目启动的时候会自动加载这些配置文件。但每个人使用就需要这么配置,太麻烦了,为了方便我们写一个公共类来优化这一问题。

    package com.dhcc.isccore.common.util;
    
    import java.io.File;
    
    import org.hyperic.sigar.Sigar;
    
    import com.dhcc.isccore.common.constant.IsccoreConstant;
    
    public class SigarUtils {
    	public final static Sigar sigar = initSigar();
    
    	public static Sigar initSigar() {
    		try {
    			File classPath = new File(IsccoreConstant.SIGAR_PATH);
    
    			String path = System.getProperty("java.library.path");
    			String sigarLibPath = classPath.getCanonicalPath();
    			// 为防止java.library.path重复加,此处判断了一下
    			if (!path.contains(sigarLibPath)) {
    				if (isOSWin()) {
    					path += ";" + sigarLibPath;
    				} else {
    					path += ":" + sigarLibPath;
    				}
    				System.setProperty("java.library.path", path);
    			}
    			return new Sigar();
    		} catch (Exception e) {
    			return null;
    		}
    	}
    
    	public static boolean isOSWin() {// OS 版本判断
    		String OS = System.getProperty("os.name").toLowerCase();
    		if (OS.indexOf("win") >= 0) {
    			return true;
    		} else
    			return false;
    	}
    }
    
    文件存放位置如下:



    jar依赖:

    <dependency>
    	<groupId>org.hyperic</groupId>
    	<artifactId>sigar</artifactId>
    	<version>1.6.5.132</version>
    </dependency>


    获取服务器信息的vo类定义:

    package com.dhcc.isccore.entity.serverinfo;
    
    import java.util.Date;
    import java.util.List;
    
    import org.hyperic.sigar.NetInterfaceConfig;
    
    public class ServerInfoVo {
    
    	private String serverIP;		//服务器IP
    	private String serverURL;		//服务器Url
    	private String serverType;		//服务器类型
    	private Date serverTime;		//服务器时间
    	
    	private String osName;			//操作系统名称
    	private String osVersion;		//操作系统版本
    	private String userName;		//用户名称
    	private String userHome;		//用户主目录
    	private String osTimeZone;		//操作系统时区
    	
    	private String memTotal;		//物理内存总量
    	private String memUsed;			//已使用的物理内存
    	private String memFree;			//物理内存剩余量
    	
    	private int cpuNum;				//cpu总数
    	private List<CpuInfoVo> cpuList;//cpu信息
    	
    	private int netNum;			//网卡总数
    	private List<NetInterfaceConfig> netList;	//网卡信息
    	
    	private String javaPath;		//java安路径
    	private String javaVendor;		//java运行时供应商
    	private String javaVersion;		//java版本
    	private String javaName;		//java运行时名称
    	private String jvmVersion;		//java虚拟机版本
    	private String jvmTotalMemory;	//java虚拟机总内存
    	private String jvmFreeMemory;	//java虚拟机剩余内存
    	
    	private String databaseType;	//数据库类型
    	private String databaseVersion;	//数据库类型
    	private String databaseDriver;	//数据库驱动
    	private String driverVersion;	//数据库驱动版本
    	private String jdbcUrl;			//数据库连接url
    	
    	public String getServerIP() {
    		return serverIP;
    	}
    	public void setServerIP(String serverIP) {
    		this.serverIP = serverIP;
    	}
    	public String getServerURL() {
    		return serverURL;
    	}
    	public void setServerURL(String serverURL) {
    		this.serverURL = serverURL;
    	}
    	public String getServerType() {
    		return serverType;
    	}
    	public void setServerType(String serverType) {
    		this.serverType = serverType;
    	}
    	public String getOsName() {
    		return osName;
    	}
    	public void setOsName(String osName) {
    		this.osName = osName;
    	}
    	public String getOsVersion() {
    		return osVersion;
    	}
    	public void setOsVersion(String osVersion) {
    		this.osVersion = osVersion;
    	}
    	public String getUserName() {
    		return userName;
    	}
    	public void setUserName(String userName) {
    		this.userName = userName;
    	}
    	public String getUserHome() {
    		return userHome;
    	}
    	public void setUserHome(String userHome) {
    		this.userHome = userHome;
    	}
    	public String getOsTimeZone() {
    		return osTimeZone;
    	}
    	public void setOsTimeZone(String osTimeZone) {
    		this.osTimeZone = osTimeZone;
    	}
    	public String getJavaVendor() {
    		return javaVendor;
    	}
    	public void setJavaVendor(String javaVendor) {
    		this.javaVendor = javaVendor;
    	}
    	public String getJavaVersion() {
    		return javaVersion;
    	}
    	public void setJavaVersion(String javaVersion) {
    		this.javaVersion = javaVersion;
    	}
    	public String getJavaName() {
    		return javaName;
    	}
    	public void setJavaName(String javaName) {
    		this.javaName = javaName;
    	}
    	public String getJvmVersion() {
    		return jvmVersion;
    	}
    	public void setJvmVersion(String jvmVersion) {
    		this.jvmVersion = jvmVersion;
    	}
    	public String getJvmTotalMemory() {
    		return jvmTotalMemory;
    	}
    	public void setJvmTotalMemory(String jvmTotalMemory) {
    		this.jvmTotalMemory = jvmTotalMemory;
    	}
    	public String getJvmFreeMemory() {
    		return jvmFreeMemory;
    	}
    	public void setJvmFreeMemory(String jvmFreeMemory) {
    		this.jvmFreeMemory = jvmFreeMemory;
    	}
    	public String getDatabaseType() {
    		return databaseType;
    	}
    	public void setDatabaseType(String databaseType) {
    		this.databaseType = databaseType;
    	}
    	public String getDatabaseVersion() {
    		return databaseVersion;
    	}
    	public void setDatabaseVersion(String databaseVersion) {
    		this.databaseVersion = databaseVersion;
    	}
    	public String getDatabaseDriver() {
    		return databaseDriver;
    	}
    	public void setDatabaseDriver(String databaseDriver) {
    		this.databaseDriver = databaseDriver;
    	}
    	public String getDriverVersion() {
    		return driverVersion;
    	}
    	public void setDriverVersion(String driverVersion) {
    		this.driverVersion = driverVersion;
    	}
    	public String getJdbcUrl() {
    		return jdbcUrl;
    	}
    	public void setJdbcUrl(String jdbcUrl) {
    		this.jdbcUrl = jdbcUrl;
    	}
    	
    	public Date getServerTime() {
    		return serverTime;
    	}
    	public void setServerTime(Date serverTime) {
    		this.serverTime = serverTime;
    	}
    	public String getJavaPath() {
    		return javaPath;
    	}
    	public void setJavaPath(String javaPath) {
    		this.javaPath = javaPath;
    	}
    	public int getCpuNum() {
    		return cpuNum;
    	}
    	public void setCpuNum(int cpuNum) {
    		this.cpuNum = cpuNum;
    	}
    	public List<CpuInfoVo> getCpuList() {
    		return cpuList;
    	}
    	public void setCpuList(List<CpuInfoVo> cpuList) {
    		this.cpuList = cpuList;
    	}
    	public int getNetNum() {
    		return netNum;
    	}
    	public void setNetNum(int netNum) {
    		this.netNum = netNum;
    	}
    	public List<NetInterfaceConfig> getNetList() {
    		return netList;
    	}
    	public void setNetList(List<NetInterfaceConfig> netList) {
    		this.netList = netList;
    	}
    	public String getMemTotal() {
    		return memTotal;
    	}
    	public void setMemTotal(String memTotal) {
    		this.memTotal = memTotal;
    	}
    	public String getMemUsed() {
    		return memUsed;
    	}
    	public void setMemUsed(String memUsed) {
    		this.memUsed = memUsed;
    	}
    	public String getMemFree() {
    		return memFree;
    	}
    	public void setMemFree(String memFree) {
    		this.memFree = memFree;
    	}
    	
    }
    

    获取服务器信息的实现类:

    public void findServerInfo(BaseAbstractDto baseAbstractDto){
    	try {
    		SigarUtils.initSigar();			//初始化动态库链接路径
    		ServerInfoDto dto = super.getExactlyDto(baseAbstractDto);
    		Properties props=System.getProperties();
    		HttpServletRequest request = ServletActionContext.getRequest();
    		Runtime runTime = Runtime.getRuntime();
    		InetAddress address = InetAddress.getLocalHost();
    			
    		//服务信息
    		ServerInfoVo serverInfoVo = new  ServerInfoVo();
    		InetAddress.getLocalHost().getHostAddress();
    		serverInfoVo.setServerIP(address.getHostAddress());
    		serverInfoVo.setServerURL(
    				request.getScheme()					//请求头
    				+"://" + address.getHostAddress() 	//服务器地址  
    				+ ":"   
    				+ request.getServerPort()        	//端口号  
    				+ request.getContextPath());      	//项目名称 
    		ServletContext application = request.getSession().getServletContext();
    		serverInfoVo.setServerType(application.getServerInfo());
    		serverInfoVo.setServerTime(new Date());
    			
    		//操作系统
    		serverInfoVo.setOsName(props.getProperty("os.name")+"  "+props.getProperty("os.arch"));
    		serverInfoVo.setOsVersion(props.getProperty("os.version"));
    		serverInfoVo.setUserName(props.getProperty("user.name"));
    		serverInfoVo.setUserHome(props.getProperty("user.home"));
    		Calendar cal = Calendar.getInstance();
    		TimeZone timeZone = cal.getTimeZone();
    		serverInfoVo.setOsTimeZone(timeZone.getDisplayName());
    			
    		//Cpu
    		Sigar sigar = new Sigar();
    	        CpuInfo cpuInfos[] = sigar.getCpuInfoList();
    	        serverInfoVo.setCpuNum(cpuInfos.length);
    	        CpuPerc cpuList[] = sigar.getCpuPercList();
    	        List<CpuInfoVo> cpuVoList = new ArrayList<CpuInfoVo>();
    	        for(int m=0;m<cpuList.length;m++){
    	        	CpuInfoVo cpuInfoVo = new CpuInfoVo();
    	        	cpuInfoVo.setCpuMhz(cpuInfos[m].getMhz());
    	        	String cpuIdle = String.format("%.2f",cpuList[m].getIdle()*100)+"%";
    	        	cpuInfoVo.setCpuLdle(cpuIdle);
    	        	String cpuCombined = String.format("%.2f",cpuList[m].getCombined()*100)+"%";
    	        	cpuInfoVo.setCpuCombined(cpuCombined);
    	        	cpuVoList.add(cpuInfoVo);
    	        }
    	        serverInfoVo.setCpuList(cpuVoList);
    	        
    	        //网卡信息
    	        String netInfos[] = sigar.getNetInterfaceList();
    	        List<NetInterfaceConfig> netList = new ArrayList<NetInterfaceConfig>();
    	        for(int i=0;i<netInfos.length;i++){
    	        	NetInterfaceConfig netInfo = sigar.getNetInterfaceConfig(netInfos[i]); 
    	        	if((NetFlags.LOOPBACK_ADDRESS.equals(netInfo.getAddress()))  	//127.0.0.1 
                            || (netInfo.getFlags() == 0)  							//标识为0
                            || (NetFlags.NULL_HWADDR.equals(netInfo.getHwaddr()))	//MAC地址不存在
                            || (NetFlags.ANY_ADDR.equals(netInfo.getAddress()))		//0.0.0.0
    	        			){
    	        		continue;
    	        	}
    	        	netList.add(netInfo);
    	        }
    	        serverInfoVo.setNetNum(netList.size());
    	        serverInfoVo.setNetList(netList);
    	        
    	        //物理内存
    	        Mem mem = sigar.getMem();  
    	        serverInfoVo.setMemTotal((mem.getTotal()/(1024*1024))+"M");
    	        serverInfoVo.setMemUsed((mem.getUsed()/(1024*1024))+"M");
    	        serverInfoVo.setMemFree((mem.getFree()/(1024*1024))+"M");
    	        
    		//java配置
    		serverInfoVo.setJavaPath(props.getProperty("java.home"));
    		serverInfoVo.setJavaVendor(props.getProperty("java.vendor"));
    		serverInfoVo.setJavaVersion(props.getProperty("java.version"));
    		serverInfoVo.setJavaName(props.getProperty("java.specification.name"));
    		serverInfoVo.setJvmVersion(props.getProperty("java.vm.version"));
    		serverInfoVo.setJvmTotalMemory((int)(runTime.totalMemory()/(1024*1024))+"M");
    		serverInfoVo.setJvmFreeMemory((int)(runTime.freeMemory()/(1024*1024))+"M");
    			
    			//数据库信息
    		findDatabase(serverInfoVo);
    		dto.setServerInfoVo(serverInfoVo);
    			
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
    //数据库连接信息
    public void findDatabase(ServerInfoVo serverInfoVo){
    	try{
    		PropertiesUtil propertiesUtil = PropertiesUtil.getInstance("hibernate.properties");//读取配置文件的连接信息
    		String driver = propertiesUtil.getProperty("jdbc.driver");
    		String jdbcUrl = propertiesUtil.getProperty("jdbc.url");
    		serverInfoVo.setJdbcUrl(jdbcUrl);
    		String username = propertiesUtil.getProperty("jdbc.username");
    		String password = propertiesUtil.getProperty("jdbc.password");
    		Class.forName(driver);
    		Connection con = (Connection) DriverManager.getConnection(jdbcUrl,username,password);
    		DatabaseMetaData metaData = (DatabaseMetaData) con.getMetaData();
    		serverInfoVo.setDatabaseType(metaData.getDatabaseProductName());
    		serverInfoVo.setDatabaseVersion(metaData.getDatabaseProductVersion());
    		serverInfoVo.setDatabaseDriver(metaData.getDriverName());
    		serverInfoVo.setDriverVersion(metaData.getDriverVersion());
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    }
    

    显示效果如下:



    上诉方案主要讲解服务器信息的实现方法,不要全抄代码,部分无关紧要的代码并未上传,仅供参考,谢谢!



    展开全文
  • JAVA获取服务器MAC地址

    千次阅读 2006-06-05 10:15:00
    最近做软件注册这块需要获取计算机的唯一标识符,能唯一识符的可以有CPU型号,硬盘序列号和网卡序列号,这里我介绍一下获取MACimport java.io.*;import java.util.*;public class mac{public static void main(String[]...

    最近做软件注册这块需要获取计算机的唯一标识符,能唯一识符的可以有CPU型号,硬盘序列号和网卡序列号,这里我介绍一下获取MAC

    import java.io.*;
    import java.util.*;
    public class mac{
    public static void main(String[] args)
            {        
            System.out.println(getMacOnWindow());
            }
    private static String getMacOnWindow() {
            String s = "";
            try {
                String s1 = "ipconfig /all";
                Process process = Runtime.getRuntime().exec(s1);
                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String nextLine;
                for (String line = bufferedreader.readLine(); line != null; line = nextLine) {
                    nextLine = bufferedreader.readLine();
                    if (line.indexOf("Physical Address") <= 0) {
                        continue;
                    }
                    int i = line.indexOf("Physical Address") + 36;
                    s = line.substring(i);
                    break;
                }
                bufferedreader.close();
                process.waitFor();
            } catch (Exception exception) {
                s = "";
            }
            return s.trim();
        }
    }
     

    希望对大家有用

    展开全文
  • Java获取服务器根目录

    千次阅读 2016-05-10 10:28:18
    获取Java服务器的根目录
  • java代码获取服务器地址

    千次阅读 2017-11-25 11:49:51
    很实用的代码:HttpServletRequest httpRequest=(HttpServletRequest)request; String strBackUrl = "http://" + request.getServerName() //服务器地址 + ":" + request.getServe
  • java服务器获得客户端IP地址

    千次阅读 2017-01-12 15:41:42
    服务器获得客户端IP地址 public static final String getIpAddress(HttpServletRequest request) { // 获取请求主机IP地址,如果通过代理进来,则透过防火墙获取真实IP地址 String ip = request.getHeader...
  • java如何获取服务器地址&端口

    千次阅读 2017-07-27 14:41:01
    public static String getLocalPort() throws MalformedObjectNameException{ MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();  Set objectNames = beanServer.queryNames(new Obje
  • Java获取当前服务器所有的IP地址 此代码会列出服务器所有的IP地址,包括IPv4和IPv6的 @Test public void s() throws SocketException { Enumeration<NetworkInterface> e = NetworkInterface....
  • JAVA获取服务器路径

    千次阅读 2012-03-04 17:39:14
    获取服务器路径  在JSF环境中获取到ServletContext:  ServletContext sc = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();  参考:...
  • JAVA获取服务器本机IP地址和MAC代码

    千次阅读 2018-03-25 23:28:44
    import java.net.InetAddress;import java.net.NetworkInterface;import java.net.SocketException;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import o...
  • * 获取本机IP */ public static String getLocalIP() { String ip = ""; try { if(isLinux()){ Enumeration<?> e1 = (Enumeration<?>) NetworkInterface .getNetworkInterfaces();
  • 1、获取Tomcat端口 MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer(); Set<ObjectName> objectNames = null; try { objectNames = beanServer .queryNames(new ObjectName("*:type=...
  • try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { Networ...
  • java 获取服务器端ip

    千次阅读 2017-09-14 11:38:48
    public static String getLocalhostIP(){ InetAddress address = null;//获取的是本地的IP地址 try { address = InetAddress.getLocalHost(); return address.getHostAddress();//192
  • request.getServletContext().getRealPath("/") 获取项目所在服务器的全路径,如:D:\Program Files\apache-tomcat-7.0.25\webapps\TestSytem\ request.getServletPath() 获取客户端请求的路径名,如:/object/...
  • 使用java库自带的OperatingSystemMXBean类获取服务器负载相关信息 这个类java文档中并没有详细说明 这里提供一个工具类   package com.bootdo.common.utils; import ...
  • 使用的环境是由平板访问服务器获得打印机列表显示到平板上,一开始是可以获取打印机列表的,但是项目启动几天之后,打印机列表总是无故消失,必须要重启项目才能再次识别到打印机列表,求大神指点。 贴上获得打印机...
  • java 获取服务器根目录

    千次阅读 2012-08-23 16:54:49
    项目中需要从一个模板里面读取信息, 那个模板文件该怎么放呢?...获取服务器的根目录 String templateurl = request.getSession().getServletContext().getRealPath("/WEB-INF/pe_template/PESurplusMaterial.xl
  • import java.net.InetAddress; import java.net.UnknownHostException; import org.hyperic.sigar.CpuInfo; import org.hyperic.sigar.CpuPerc; import org.hyperic.sigar.FileSystem; import ...
  • JAVA + Struts2获取服务器地址

    千次阅读 2011-08-26 10:02:04
    //访问服务器所带有的参数信息 String queryInfo=request.getQueryString(); if(queryInfo!=null&&(!queryInfo.equals(""))){ actionPath=actionPath+"?"+queryInfo; } logger.debug("用户访问的路径是:"+...
  • JAVA获取服务器路径的方法

    千次阅读 2017-03-22 16:03:33
    1、在JSF环境中获取到ServletContext: ServletContext sc = (ServletContext)FacesContext. getCurrentInstance().getExternalContext().getContext(); 2、servlet中获得项目绝对路径String filePath=this....
  • 获取当前服务器IP地址 import java.net.*; import java.util.*; //获取当前服务器IP public class IPdemo { public static void main(String[] args) { System.out.println(getMyIp()); } @SuppressWarnings...
  • java获取服务器启动时间

    千次阅读 2020-03-25 17:48:44
    public static Date getServerStqrtTime(){ long time = ManagementFactory.getRuntimeMXBean().... } 获取JDK名称 java.lang.management.ManagementFactory.getRuntimeMXBean().getVmName() 上面其实是JDK启动时间
  • java获取服务器真实ip

    千次阅读 2019-05-22 20:00:52
    * 获取用户真实IP地址,不使用request.getRemoteAddr();的原因是有可能用户使用了代理软件方式避免真实IP地址, * * 可是,如果通过了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP值,究竟哪个...
  • Java里面获取当前服务器的IP地址

    万次阅读 2019-04-22 10:29:08
    java获取当前服务器地址主要使用到InetAddress这个类 public static void main(String[] args) { try { //用 getLocalHost() 方法创建的InetAddress的对象 InetAddress address = InetAddress.getLocalHo.....
  • 不传参数,直接通过ServletRequestAttributes获取当前项目所在服务器的端口号和ip地址 public static String getUrl(){ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) ...
  • * desc:获取服务器Ipv4地址 * */ public static String getInet4Address() { Enumeration<NetworkInterface> nis; String ip = null; try { nis = NetworkInterface...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 783,601
精华内容 313,440
关键字:

java获取服务器地址

java 订阅