package com.mashensoft.net;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Scanner;
/**
*
* @author PeicongHe
*
*/
public class GetIpDemo {
/**
* 从网页上获取数据
* @param myUrl URL地址
* @param charset 编码方式
* @return 网页上的数据,String类型
*/
public static String getContentFromUrl(String myUrl,String charset){
StringBuffer sb = new StringBuffer();
URL url;
try {
url = new URL(myUrl);
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
Scanner sc = new Scanner(is,charset);
while(sc.hasNextLine()){
sb.append(sc.nextLine()).append("\r\n");
}
sc.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("执行成功");
return sb.toString();
}
/**
* 功能:保存数据到文件中
* @param content 要保存的内容
* @param fileName 目标文件名(路径)
* @return boolean 执行是否成功 trun/false
*/
public static boolean writeContentToFile(String content,String fileName){
boolean sign = false;
PrintWriter pw;
try {
pw = new PrintWriter(fileName);
pw.println(content);
pw.flush();
pw.close();
sign = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
sign = false;
}
return sign;
}
/**
* 从文件中读取数据
* @param fileName(文件路径)
* @return content
*/
public static String getContentFromFile (String fileName){
StringBuffer content = new StringBuffer();
try {
Scanner sc = new Scanner(new FileInputStream(fileName));
while(sc.hasNextLine()){
content.append(sc.nextLine()).append("\r\n");
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return content.toString();
}
/**
* 阅读并显示文件
* @param fileName 文件名
*/
public static void readerFile(String fileName){
char[] myArray = new char[3];
int len;
try {
Reader reader = new InputStreamReader(new FileInputStream(fileName));
while((len=reader.read(myArray))!=-1){
System.out.print(new String(myArray));
}
}
catch (FileNotFoundException e1) {
e1.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 实现截取字符串中部分字符(例子)
* int java.lang.String.indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
*/
public static void test(){
String name = "hepeicong";
System.out.println(name.indexOf("e"));
System.out.println(name.lastIndexOf("e"));
int beginIndex = name.indexOf("e");
int endIndex = name.lastIndexOf("e");
String namex = name.substring(beginIndex+1,endIndex);
System.out.println(namex);
}
/**
* 功能:从文本里获取IP地址
* @param content
* @return ip
*/
public static String getIpFromContent(String content){
int beginIndex = content.indexOf("[");
int endIndex = content.indexOf("]");
String ip = content.substring(beginIndex,endIndex);
return ip ;
}
/**
* 功能:从文本内容里获取运营商名称
* @param content
* @return ad
*/
public static String getSpFromContent(String content){
int beginIndex = content.indexOf("来自:");
int endIndex = content.indexOf("</center>");
String ad = content.substring(beginIndex,endIndex);
return ad;
}
public static void main(String[] args){
String content = getContentFromUrl("http://1212.ip138.com/ic.asp","gb2312");
writeContentToFile(content,"c.html");
System.out.println(getIpFromContent(content));
System.out.println(getSpFromContent(content));
}
}