當前位置:首頁 » 編程語言 » javaip

javaip

發布時間: 2022-02-06 21:57:57

Ⅰ IP 地址排序java

ip.txt就按LZ給的,排序完成後在控制台會輸出排序後的,ip.txt裡面也會變成排序後的結果,
完整代碼(請看注釋):
//Test.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
public static void main(String[] args) throws IOException {
//讀入ip.txt文件 我放C盤下面,你可以自己定義位置
BufferedReader br = new BufferedReader(new FileReader("c:/ip.txt"));
List <String> list = new ArrayList<String>();
String str;
//將每條ip都讀入list(ip.txt裡面,一條IP一行)
while((str=br.readLine())!=null)
list.add(str);

//採用IPComparator的演算法進行排序
Collections.sort(list,IPComparator);

//控制輸出一下,如果不需要 你可以去掉
for(Object o :list)
System.out.println((String)o);
br.close();

//把排序好的,輸出回ip.txt
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/ip.txt"));
for(Object o:list)
{
bw.write((String)o+"\r\n");
bw.flush();
}
bw.close();
}

//後面的內容都是IPComparator演算法的東西。
public static int compartTo(String ip1,String ip2){
long[] ip11=parseIp(ip1);
long[] ip22=parseIp(ip2);
long ip1Result=0,ip2Result=0;
for(int i=0;i<4;i++){
ip1Result+=(ip11[i]<<(24-i*8));
}
for(int i=0;i<4;i++){
ip2Result+=(ip22[i]<<(24-i*8));
}
if(ip1Result-ip2Result>0){
return 1;
}else if(ip1Result-ip2Result<0){
return -1;
}else{
return 0;
}
}

public static Comparator IPComparator=new Comparator(){
public int compare(Object ip1, Object ip2) {
return compartTo((String)ip1,(String)ip2);
}
};

private static long[] parseIp(String ip){
ip=ip.replace(".", "#");
long result[]=new long[4];
String[] ip1=ip.split("#");
if(ip!=null){
result[0]=Long.parseLong(ip1[0]);
result[1]=Long.parseLong(ip1[1]);
result[2]=Long.parseLong(ip1[2]);
result[3]=Long.parseLong(ip1[3]);
}
return result;
}

}

Ⅱ JAVA如何限制用戶IP地址

java web中限制訪問的ip,主要是使用session的getRemortIP(HttpServletRequest)方法,如下代碼:

/**
*
*@paramrequest
*@return
*
*功能:得到用戶Ip地址
*/
publicstaticStringgetRemortIP(HttpServletRequestrequest){
if(request.getHeader("x-forwarded-for")==null){
returnrequest.getRemoteAddr();
}
returnrequest.getHeader("x-forwarded-for");
}

調用

Stringip=getRemortIP(HttpServletRequest);
if(ip=="127.0.0.1"){
System.out.print("禁止訪問");
}

Hr完整項目中---common.jsp得到網址

<%
//路徑
Stringpath=request.getContextPath();
//request保存
request.setAttribute("path",path);
%>

Ⅲ java 如何驗證ip地址

可以使用正則表達式驗證ip地址,ip地址分為v4和v6兩個版本,v4為32位,分4段,中間用.隔開,v6為128位,可分為4段32位中間用::隔開。

以下是驗證類詳細代碼:
import java.util.regex.Pattern;
/**
* A collection of utilities relating to InetAddresses.
*/
public class InetAddressUtils {
public static void main(String[] args){
String addr="192.168.1.2";
System.out.println(isIPv4Address(addr));
}

private static final Pattern IPV4_PATTERN =
Pattern.compile(
"^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");

private static final Pattern IPV6_STD_PATTERN =
Pattern.compile(
"^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$");

private static final Pattern IPV6_HEX_COMPRESSED_PATTERN =
Pattern.compile(
"^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$");

public static boolean isIPv4Address(final String input) {
return IPV4_PATTERN.matcher(input).matches();
}

public static boolean isIPv6StdAddress(final String input) {
return IPV6_STD_PATTERN.matcher(input).matches();
}

public static boolean isIPv6HexCompressedAddress(final String input) {
return IPV6_HEX_COMPRESSED_PATTERN.matcher(input).matches();
}

public static boolean isIPv6Address(final String input) {
return isIPv6StdAddress(input) || isIPv6HexCompressedAddress(input);
}
}

Ⅳ java如何更改IP地址

更改IP地址之前,先獲取本機的IP地址
public static String getIP(){
String Ip = null;
try {
Ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
return Ip;
}

獲取到本機ip以後,在進行修改ip地址
// local - 介面名稱
// static - 設置使用本地靜態配置設置IP地址。
// 10.0.0.9 - 要修改的ip
// 255.0.0.0 - 子網掩碼
// 10.0.0.1 - 網關,如果為none: 不設置默認網關。
// 1 -默認網關的躍點數。如果網關設置為 』none』,則不應設置此欄位。
public static void setIP(String newip) throws Exception {
Runtime.getRuntime().exec("netsh interface ip set addr \"本地連接\" static "
+ newip + " 255.0.0.0 10.0.0.1 1");
}

Ⅳ 用JAVA如何實現IP綁定

package src;

import java.io.*;

public class getMac {
public static void main(String[] args) {
try {
Process process = Runtime.getRuntime().exec("ipconfig /all");
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
while ((line = input.readLine()) != null)
if (line.indexOf("Physical Address") > 0) {
String MACAddr = line.substring(line.indexOf("-") - 2);
System.out.println("MAC address = [" + MACAddr + "]");
}
} catch (java.io.IOException e) {
System.err.println("IOException " + e.getMessage());
}
}
}

ipconfig是Windows下命令提示符支持的一個命令,可以查詢到你的機器的ip等網路配置

Runtime.getRuntime().exec("ipconfig /all"); 就是執行該命令

if (line.indexOf("Physical Address") > 0)表示如果在line中查找到Physical Address,就繼續執行if中的語句,否則如果找不到,line.indexOf("Physical Address")的返回值=-1

請多少給點分,謝謝

Ⅵ java IP查詢方法

Java編程查詢IP地址歸屬地,可以調用淘寶提供的service查詢,並且解析http請求返回的json串,代碼如下:

packagegetAddressByIp;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.net.HttpURLConnection;
importjava.net.MalformedURLException;
importjava.net.URL;
importnet.sf.json.JSONObject;

publicclassGetAddressByIp
{
/**
*
*@paramIP
*@return
*/
(StringIP){
Stringresout="";
try{
Stringstr=getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);
System.out.println(str);
JSONObjectobj=JSONObject.fromObject(str);
JSONObjectobj2=(JSONObject)obj.get("data");
Stringcode=(String)obj.get("code");
if(code.equals("0")){
resout=obj2.get("country")+"--"+obj2.get("area")+"--"+obj2.get("city")+"--"+obj2.get("isp");
}else{
resout="IP地址有誤";
}
}catch(Exceptione){

e.printStackTrace();
resout="獲取IP地址異常:"+e.getMessage();
}
returnresout;

}

(StringurlStr)
{
try
{//獲取HttpURLConnection連接對象
URLurl=newURL(urlStr);
HttpURLConnectionhttpConn=(HttpURLConnection)url.openConnection();
//設置連接屬性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
//獲取相應碼
intrespCode=httpConn.getResponseCode();
if(respCode==200)
{
returnConvertStream2Json(httpConn.getInputStream());
}
}
catch(MalformedURLExceptione)
{
e.printStackTrace();
}
catch(IOExceptione)
{
e.printStackTrace();
}
return"";
}

(InputStreaminputStream)
{
StringjsonStr="";
//ByteArrayOutputStream相當於內存輸出流
ByteArrayOutputStreamout=newByteArrayOutputStream();
byte[]buffer=newbyte[1024];
intlen=0;
//將輸入流轉移到內存輸出流中
try
{
while((len=inputStream.read(buffer,0,buffer.length))!=-1)
{
out.write(buffer,0,len);
}
//將內存流轉換為字元串
jsonStr=newString(out.toByteArray());
}
catch(IOExceptione)
{
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnjsonStr;
}
}

Ⅶ java怎麼獲取請求的ip

java獲取外網ip地址方法:
public class Main {

public static void main(String[] args) throws SocketException {
System.out.println(Main.getRealIp());
}

public static String getRealIp() throws SocketException {
String localip = null;// 本地IP,如果沒有配置外網IP則返回它
String netip = null;// 外網IP

Enumeration<NetworkInterface> netInterfaces =
NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
boolean finded = false;// 是否找到外網IP
while (netInterfaces.hasMoreElements() && !finded) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
ip = address.nextElement();
if (!ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 外網IP
netip = ip.getHostAddress();
finded = true;
break;
} else if (ip.isSiteLocalAddress()
&& !ip.isLoopbackAddress()
&& ip.getHostAddress().indexOf(":") == -1) {// 內網IP
localip = ip.getHostAddress();
}
}
}

if (netip != null && !"".equals(netip)) {
return netip;
} else {
return localip;
}
}
}

Ⅷ java獲得IP地址

簡單實現代碼如下:
js獲取來源頁地址方法:
var url = document.referrer;
document.write(url);
jsp獲取來源頁地址方法:
String url = request.getHeader(」Referer」);
System.out.println(url);
對比兩個方法:
1.js里是」referrer」,jsp里是」referer」,前者比後者多一個」r」;
2.前者如直接輸入網址,則顯示為空,後者顯示null;
import java.net.*;

public class ip5 {

public static void main(String args[]) throws Exception {

String ip = InetAddress.getLocalHost().getHostAddress();

System.out.println(ip);

}

}

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:336
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:378
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:612
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:32
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:107
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:944
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:741
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:803
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:511
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:372