在GWT和Google App Engine中获取客户端IP地址,可以通过以下方法实现:
com.google.gwt.user.client.Window.Location.getParameter("ip")
方法获取客户端IP地址。com.google.appengine.api.utils.SystemProperty.getRemoteAddr()
方法获取客户端IP地址。以下是一个简单的示例代码:
public class IPAddress {
public static String getClientIP() {
String ipAddress = "";
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
ipAddress = SystemProperty.getRemoteAddr();
} else {
ipAddress = Window.Location.getParameter("ip");
}
return ipAddress;
}
}
在这个示例代码中,我们首先检查当前环境是否为生产环境,如果是生产环境,则使用SystemProperty.getRemoteAddr()
方法获取客户端IP地址;如果不是生产环境,则使用Window.Location.getParameter("ip")
方法获取客户端IP地址。
需要注意的是,在Google App Engine中,SystemProperty.getRemoteAddr()
方法只能在生产环境中使用,因此在开发环境中需要使用其他方法获取客户端IP地址。
领取专属 10元无门槛券
手把手带您无忧上云