在WebService中处理IP地址
//在webService中写到:
import javax.annotation.Resource;
import
javax.servlet.http.HttpServletRequest;
import
javax.xml.ws.WebServiceContext;
import
javax.xml.ws.handler.MessageContext;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import
org.springframework.beans.factory.annotation.Autowired;
@Resource
private WebServiceContext context;
容器自动把WebSericeContext 注入到该属性中.
public void getIpAddress(){
MessageContext ctx = context.getMessageContext();
HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);
String ip=request.getRemoteAddr(); //获得地址
}
附:通过IP获得详细的市和县:
//先写这个类,这是通过网上的一个接口进行IP查询所在地
import java.io.BufferedReader;
import
java.io.DataOutputStream;
import java.io.IOException;
import
java.io.InputStreamReader;
import java.net.HttpURLConnection;
import
java.net.URL;
/**
* @author 丁宗彪
* @version 创建时间: 2014-5-8
下午04:18:45
*/
public class AddressByIpUtil {
public static String getIpAddr(HttpServletRequest request)
{
String ip =
request.getHeader("x-forwarded-for");
if(ip ==
null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
*
* @param content
* 请求的参数
格式为:name=xxx&pwd=xxx
* @param encoding
*
服务器端请求编码。如GBK,UTF-8等
* @return
*/
public static
String getAddresses(String content, String encodingString)
{
content="ip="+content;
//
这里调用pconline的接口
String urlStr = "http://whois.pconline.com.cn/ipJson.jsp";
//
从http://whois.pconline.com.cn取得IP所在的省市区信息
String
returnStr = AddressByIpUtil.getResult(urlStr, content,
encodingString);
if (returnStr != null) {
//
处理返回的省市区信息
int startIndex =
returnStr.indexOf("\"pro");
int endIndex =
returnStr.indexOf(",\"addr");
if (startIndex > 0
&& endIndex > 0 && endIndex > startIndex)
{
returnStr = returnStr.substring(startIndex,
endIndex);
returnStr = returnStr.replaceAll("\"",
"").replace("pro:", "")
.replace("city:",
"").replace("region:",
"");
}
}
return
returnStr.substring(0,returnStr.length()-1);
}
/**
* @param urlStr
*
请求的地址
* @param content
* 请求的参数
格式为:name=xxx&pwd=xxx
* @param encoding
*
服务器端请求编码。如GBK,UTF-8等
* @return
*/
private static
String getResult(String urlStr, String content, String encoding)
{
URL url = null;
HttpURLConnection connection =
null;
try {
url = new
URL(urlStr);
connection = (HttpURLConnection)
url.openConnection();//
新建连接实例
connection.setConnectTimeout(2000);//
设置连接超时时间,单位毫秒
connection.setReadTimeout(2000);//
设置读取数据超时时间,单位毫秒
connection.setDoOutput(true);// 是否打开输出流
true|false
connection.setDoInput(true);//
是否打开输入流true|false
connection.setRequestMethod("POST");//
提交方法POST|GET
connection.setUseCaches(false);//
是否缓存true|false
connection.connect();// 打开连接端口
DataOutputStream out = new
DataOutputStream(connection
.getOutputStream());//
打开输出流往对端服务器写数据
out.writeBytes(content);// 写数据,也就是提交你的表单
name=xxx&pwd=xxx
out.flush();//
刷新
out.close();// 关闭输出流
BufferedReader
reader = new BufferedReader(new
InputStreamReader(
connection.getInputStream(),
encoding));// 往对端写完数据对端服务器返回数据
//
,以BufferedReader流来读取
StringBuffer buffer = new
StringBuffer();
String line =
"";
while ((line = reader.readLine()) != null)
{
buffer.append(line);
}
reader.close();
return
buffer.toString();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (connection != null)
{
connection.disconnect();//
关闭连接
}
}
return
null;
}
}
//这是测试类
public static void main(String[] args) throws Exception
{
AddressByIpUtil addressUtils = new
AddressByIpUtil();
// 测试ip
String ip =
"111.204.22.226";
String address = addressUtils.getAddresses(ip,
"GBK");
System.out.println(address);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。