ip解析

1.第一种ip解析方式

java通过url获取网页内容
https://blog.csdn.net/hlk_1135/article/details/53968002
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
//第一种ip解析方式
public class test1 {

        public static void main(String[] args) {
            try {
                //建立连接
                //上海 114.80.166.240
                //江苏 112.23.23.23
                URL url = new URL("http://ip.taobao.com/service/getIpInfo.php?ip=114.80.166.240");
                HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();
                httpUrlConn.setDoInput(true);
                httpUrlConn.setRequestMethod("GET");
                httpUrlConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                //获取输入流
                InputStream input = httpUrlConn.getInputStream();
                //将字节输入流转换为字符输入流
                InputStreamReader read = new InputStreamReader(input, "utf-8");
                //为字符输入流添加缓冲
                BufferedReader br = new BufferedReader(read);
                // 读取返回结果
                String data = br.readLine();
                JSONObject jo = new JSONObject(data);
                System.out.println(data);
                String data_value = jo.getString("data");
                System.out.println(data_value);

                JSONObject jo2 = new JSONObject(data_value);

                String country=jo2.getString("country");
                String region=jo2.getString("region");
                String city=jo2.getString("city");
                String isp=jo2.getString("isp");

                System.out.println("国家/地区"+"\t"+country);
                System.out.println("省份"+"\t"+region);
                System.out.println("城市"+"\t"+city);
                System.out.println("运营商"+"\t"+isp);
//结果
               /* 国家/地区    中国
                省份 上海
                城市 上海
                运营商    电信*/
                // 释放资源
                br.close();
                read.close();
                input.close();
                httpUrlConn.disconnect();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
}

2

//getIntSum();
File file = new File("C://income.txt");
//char simi = ':';
//char simi1 = ' ';
char fm1 = ',';
int b = 0;
for (b = 1; b <= 100000; b++) {
    try {
        OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8");
        BufferedWriter bw = new BufferedWriter(fw);
        //先写入b也就是序号id
        bw.append(String.valueOf(b));
        bw.append(fm1);
        bw.append(getGZ());
        bw.append(fm1);

        bw.append(getElseIn());
        bw.append("\r");

        bw.flush();
        fw.flush();

        bw.close();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

    }
}

随机生产ip

public static String getRandomIp() {

    // ip范围
    int[][] range = { { 607649792, 608174079 }, // 36.56.0.0-36.63.255.255
            { 1038614528, 1039007743 }, // 61.232.0.0-61.237.255.255
            { 1783627776, 1784676351 }, // 106.80.0.0-106.95.255.255
            { 2035023872, 2035154943 }, // 121.76.0.0-121.77.255.255
            { 2078801920, 2079064063 }, // 123.232.0.0-123.235.255.255
            { -1950089216, -1948778497 }, // 139.196.0.0-139.215.255.255
            { -1425539072, -1425014785 }, // 171.8.0.0-171.15.255.255
            { -1236271104, -1235419137 }, // 182.80.0.0-182.92.255.255
            { -770113536, -768606209 }, // 210.25.0.0-210.47.255.255
            { -569376768, -564133889 }, // 222.16.0.0-222.95.255.255
    };

    Random rdint = new Random();
    int index = rdint.nextInt(10);
    String ip = num2ip(range[index][0] + new Random().nextInt(range[index][1] - range[index][0]));
    return ip;
}

/*
 * 将十进制转换成IP地址
 */
public static String num2ip(int ip) {
    int[] b = new int[4];
    String x = "";
    b[0] = (int) ((ip >> 24) & 0xff);
    b[1] = (int) ((ip >> 16) & 0xff);
    b[2] = (int) ((ip >> 8) & 0xff);
    b[3] = (int) (ip & 0xff);
    x = Integer.toString(b[0]) + "." + Integer.toString(b[1]) + "." + Integer.toString(b[2]) + "." + Integer.toString(b[3]);

    return x;
}

public static void main(String[] args) {
    int count = 10000;
    for (int i = 0; i < count; i++) {
        String randomIp = getRandomIp();
        System.err.println(randomIp);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值