package com.net; import java.io.*; import java.net.*; import java.util.Date; import java.util.HashMap; import java.util.Map; public class JavaNet { /** * 根据域名获取IP * * @param realmName 域名 * @return IP */ public static String GetIP(String realmName) { InetAddress address; try { address = InetAddress.getByName(realmName); } catch (UnknownHostException e) { return null; } return address.getHostAddress(); } /** * 获取端口是否被占用 * * @param host IP * @param port 端口号 * @return 端口的使用状态 */ public static boolean PortIsUsed(String host, int port) { try { Socket socket = new Socket(host, port); return true; } catch (UnknownHostException e) { } catch (IOException e) { } return false; } /** * 获取本机IP * * @return 本机IP地址 * @throws UnknownHostException 异常 */ public static String LocalHostAddress() throws UnknownHostException { InetAddress address = InetAddress.getLocalHost(); return address.getHostAddress(); } /** * 获取本机的主机名 * * @return 主机名 * @throws UnknownHostException 异常 */ public static String LocalHostName() throws UnknownHostException { InetAddress address = InetAddress.getLocalHost(); return address.getHostName(); } /** * 获取远程文件大小 * * @param fileLocal 文件路径 * @return 文件大小,单位为bytes * @throws IOException 异常 */ public static int getHostFileSize(String fileLocal) throws IOException { int size; URL url = new URL(fileLocal); URLConnection conn = url.openConnection(); size = conn.getContentLength(); conn.getInputStream().close(); return size; } /** * 获取文件的最后修改日期 * * @param fileLocal 文件路径 * @return 最后修改日期 * @throws IOException 异常 */ public static Date getFileLastEditDate(String fileLocal) throws IOException { URL url = new URL(fileLocal); URLConnection conn = url.openConnection(); conn.setUseCaches(false); long timestamp = conn.getLastModified(); conn.getInputStream().close(); Date date = new Date(timestamp); return date; } /** * 网页抓取 * * @param local 抓取的路径 * @param htmlName html名 可为空 * @return 抓取的结果 * @throws IOException 异常 */ public static String getHtmlText(String local, String htmlName) throws IOException { if (!local.contentEquals("http://")) return "local error,like http://www.xxx.com"; if (htmlName == null) htmlName = "data.html"; URL url = new URL(local); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter writer = new BufferedWriter(new FileWriter(htmlName)); String line; StringBuffer stringBuffer = new StringBuffer(); while ((line = reader.readLine()) != null) { stringBuffer.append("\n" + line); writer.write(line); writer.newLine(); } String result = stringBuffer.toString().replaceFirst("\n", ""); reader.close(); writer.close(); return result; } /** * 获取相应头信息 * * @param local 请求路径 携带http:// * @return 响应头信息 * @throws IOException 异常 */ public static Map getHeaders(String local) throws IOException { URL url = new URL(local); URLConnection conn = url.openConnection(); Map headers = conn.getHeaderFields(); return headers; } /** * 解析URL * * @param local 请求路径 * @return url信息 * @throws MalformedURLException 异常 */ public static Map getURLInfo(String local) throws MalformedURLException { URL url = new URL(local); Map map = new HashMap(); map.put("url", url.toString()); // 协议 map.put("protocol", url.getProtocol()); // 文件名 map.put("file", url.getFile()); // 主机 map.put("host", url.getHost()); // 路径 map.put("path", url.getPath()); // 端口号 map.put("port", url.getPort()); // 默认端口号 map.put("defaultPort", url.getDefaultPort()); return map; } }