我本机的IP地址是:

关于获取本机IP地址的几种方法_java

使用代码获取:

%@ page import="java.net.InetAddress" %>
<%@ page import="java.net.UnknownHostException" %>
<%@ page import="java.net.InetAddress" %>
<%@ page import="java.io.PrintWriter" %>

<%
    /*设置编码*/
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
    final PrintWriter writer = response.getWriter();


    writer.println("获得客户端IP地址:" + request.getLocalAddr() + "<br/>");
    writer.println("获取本机的IP地址:" + request.getRemoteAddr() + "<br/>");

    InetAddress address = null;
    try {
        address = InetAddress.getLocalHost();
        //获取计算机名称和ip地址
        writer.println("获取计算机名称和ip地址:" + address + "<br/>");
        String hostAddress = address.getHostAddress();
        //获取ip地址
        writer.println("获取本机的IPV4地址:" + hostAddress + "<br/>");
        String hostName = address.getHostName();
        //获取计算机名称
        writer.println("获取计算机名称:" + hostName + "<br/>");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
%>

打印的结果是:

关于获取本机IP地址的几种方法_计算机名_02