java类中 获取服务器的IP 端口

struts2 获取request
HttpServletRequest requet=ServletActionContext.getRequest();
requet.getScheme()+"://"+requet.getServerName()+":"+requet.getServerPort()
(例如)结果为:http://localhost:8080/
 
request.getHeader("User-Agent");    //就是取得客户端的系统版本     
request.getRemoteAddr();    //取得客户端的IP     
request.getRemoteHost()     //取得客户端的主机名     
request.getRemotePort();    //取得客户端的端口     
request.getRemoteUser();    //取得客户端的用户     
request.getLocalAddr();    //取得服务器IP     
request.getLocalPort();    //取得服务器端口
 
1、JSP中获得当前应用的相对路径和绝对路径
  根目录所对应的绝对路径:request.getRequestURI()
  文件的绝对路径  :application.getRealPath(request.getRequestURI());
  当前web应用的绝对路径 :application.getRealPath("/");
  取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()
 
2 Servlet中获得当前应用的相对路径和绝对路径
  根目录所对应的绝对路径:request.getServletPath();
  文件的绝对路径 :request.getSession().getServletContext().getRealPath(request.getRequestURI())   
  当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
  (ServletContext对象获得几种方式:
  javax.servlet.http.HttpSession.getServletContext()
  javax.servlet.jsp.PageContext.getServletContext()
  javax.servlet.ServletConfig.getServletContext()
  )
 
3.Java类中获得绝对路径
  根据java.io.File的Doc文挡,可知: 默认情况下new File("/")代表的目录为:System.getProperty("user.dir")。
 
 
*************************************
JAVA应用程序获取当前路径
 
如果对于类不在包内:利用System.getProperty("user.dir")和this.getClass().getResource("")获取的路径是一样的,
但是对于类在包内的情况就不同了:System.getProperty("user.dir")取得的是包所在的路径,而this.getClass().getResource("")才真正获得的是当前类所在的路径。
 
例如: 在ddhxd.test.Test1类中:
    System.getProperty("user.dir")                  返回D:/Java/testcode
    Test1.class.getResource("")                     返回file:/D:/Java/testcode/ddhxd/test/
    **Test1.class 即 Class.forName("ddhxd.test.Test1")
--------------------------------------------------------------
System.getProperty("java.version")                  1.6.0_10                                                        // Java 运行时环境供应商 
System.getProperty("java.vendor")                   Sun Microsystems Inc.                                           //  Java 运行时环境供应商
System.getProperty("java.vendor.url")               http://java.sun.com/                                            // Java 供应商的 URL 
System.getProperty("java.home")                     D:/Java/jdk1.6.0_10/jre                                         // Java 安装目录 
System.getProperty("java.vm.specification.version") 1.0                                                             // Java 虚拟机规范版本 
System.getProperty("java.vm.specification.vendor")  Sun Microsystems Inc.                                           //  Java 虚拟机规范供应商 
System.getProperty("java.vm.specification.name")    Java Virtual Machine Specification                              //  Java 虚拟机规范名称 
System.getProperty("java.vm.version")               11.0-b15                                                        //  Java 虚拟机实现版本 
System.getProperty("java.vm.vendor")                Sun Microsystems Inc.                                           //  Java 虚拟机实现供应商 
System.getProperty("java.vm.name")                  Java HotSpot(TM) Client VM                                      //  Java 虚拟机实现名称 
System.getProperty("java.specification.version")    1.6                                                             //  Java 运行时环境规范版本 
System.getProperty("java.specification.vendor")     Sun Microsystems Inc.                                           //  Java 运行时环境规范供应商 
System.getProperty("java.specification.name")       Java Platform API Specification                                 //  Java 运行时环境规范名称 
System.getProperty("java.class.version")            50.0                                                            //  Java 类格式版本号 
System.getProperty("java.class.path")               .;D:/Java/apache-tomcat-6.0.18/lib/servlet-api.jar;.........    //  Java 类路径 
System.getProperty("java.library.path")             D:/Java/jdk1.6.0_10/bin;.;C:/WINDOWS/Sun/...................    //  加载库时搜索的路径列表 
System.getProperty("java.io.tmpdir")                C:/DOCUME~1/HUANG~1.XIA/LOCALS~1/Temp/                          //  默认的临时文件路径 
System.getProperty("java.compiler")                 null                                                            //  要使用的 JIT 编译器的名称 
System.getProperty("java.ext.dirs")                 D:/Java/jdk1.6.0_10/jre/lib/ext;C:/WINDOWS/Sun/Java/lib/ext     //  一个或多个扩展目录的路径 
System.getProperty("os.name")                       Windows XP                                                      //  操作系统的名称 
System.getProperty("os.arch")                       x86                                                             //  操作系统的架构 
System.getProperty("os.version")                    5.1                                                             //  操作系统的版本 
System.getProperty("file.separator")                /                                                               //  文件分隔符(在 UNIX 系统中是“/”) 
System.getProperty("path.separator")                ;                                                               //  路径分隔符(在 UNIX 系统中是“:”) 
System.getProperty("line.separator")                                                                                //  行分隔符(在 UNIX 系统中是“/n”) 
System.getProperty("user.name")                     Huang.XiaoDong                                                  //  用户的账户名称 
System.getProperty("user.home")                     C:/Documents and Settings/huang.xiaodong                        //  用户的主目录 
System.getProperty("user.dir")                      D:/Java/testcode                                                //  用户的当前工作目录
原文地址:https://www.cnblogs.com/huapox/p/3516216.html