{ //了解URL的构成:protocol://host:port/path?query#ref //其次再结合API就知道返回的结果是什么了 URL url = new URL("http://www.w3cschool.cc/index.html?language=cn#j2se"); //这里tostring调用是protected String toExternalForm(URL u)方法返回代表这个url的string System.out.println("URL is " + url.toString()); System.out.println("protocol is " + url.getProtocol()); System.out.println("authority is " + url.getAuthority()); System.out.println("file name is " + url.getFile()); System.out.println("host is " + url.getHost()); System.out.println("path is " + url.getPath()); System.out.println("port is " + url.getPort()); System.out.println("default port is " + url.getDefaultPort()); System.out.println("query is " + url.getQuery()); System.out.println("ref is " + url.getRef()); }catch(IOException e) e.printStackTrace();
下面的内容主要是关于URL构成以及url重写tostring方法的分析:
/*question one:
		 * What is the difference between the getHost and getAuthority methods in the URL class?
			To really understand this, you should read URI specification - RFC 2396.
			The short answer is that the authority component consists of the host component together with an optional port number, 
			username and password ... depending on the URL scheme that is used.
			question two:
			URL构成
			protocol://host:port/path?query#ref
			protocols(协议)可以是 HTTP, HTTPS, FTP, 和File。port 为端口号。path为文件路径及文件名。
			HTTP协议的URL实例如下:
			http://www.w3cschool.cc/index.html?language=cn#j2se
			以上URL实例并未指定端口,因为HTTP协议默认的端口号为80。
		//根据以上两个知识点展开,传入一个url以后,根据其构成,每读取一部分将其存储到缓冲区中,最后调用stringbuffeer.tostring.
		//当然也可以是stringbuilder的方法
		protected String toExternalForm(URL u) {
	        //String:getProtocol()-->Gets the protocol name of this URL.
	        int len = u.getProtocol().length() + 1;
	        //String:getAuthority()-->Gets the authority part of this URL.
	        if (u.getAuthority() != null && u.getAuthority().length() > 0)
	            len += 2 + u.getAuthority().length();
	        if (u.getPath() != null) {
	            len += u.getPath().length();
	        if (u.getQuery() != null) {
	            len += 1 + u.getQuery().length();
	        if (u.getRef() != null)
	            len += 1 + u.getRef().length();
	        StringBuffer result = new StringBuffer(len);
	        result.append(u.getProtocol());
	        result.append(":");
	        if (u.getAuthority() != null && u.getAuthority().length() > 0) {
	            result.append("//");
	            result.append(u.getAuthority());
	        if (u.getPath() != null) {
	            result.append(u.getPath());
	        if (u.getQuery() != null) {
	            result.append('?');
	            result.append(u.getQuery());
	        if (u.getRef() != null) {
	            result.append("#");
	            result.append(u.getRef());
	        return result.toString();

这里添加两段使用URL读取网页内容的代码:

第一段代码的主要作用是感受url的一些常规函数,特别需要注意注释里面的内容

package com.imooc.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
 * 使用URL读取网页内容
public class Test03 {
	public static void main(String[] args) {
		try {
			 * 首先调用是URL类里面openStream获取对象资源的字节输入流
			 * 直接输入流要转换成字符输入流
			 * 为了提高输入输出的效率要使用缓冲区BufferedReader
			//创建一个URL实例
			URL url = new URL("http://www.baidu.com");
			//通过URL的openStream方法获取URL对象所表示的资源的字节输入流
			 * 在观看API的时候一定要注意其本身返回的对象是哪种类型
			 * InputStream返回的是一个字节流
			InputStream is = url.openStream();
			//将字节输入流转换为字符输入流
			InputStreamReader isr = new InputStreamReader(is, "utf-8");
			//为字符输入流添加缓冲区。缓冲区使用是为了提高输入和输出的效率
			BufferedReader br = new BufferedReader(isr);
			String data = br.readLine();//读取数据
			while (data != null) {//循环读取数据
				System.out.println(data);//输出数据
				data = br.readLine();
			br.close();
			isr.close();
			is.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();

示例二:主要作用是结合正则表达式抓取网页的某部分内容
package com.bjsxt.regex.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 * 网络爬虫 第一步:取链接,访问链接 第二步:递归下载链接
 * 1.URL类详解
 * java.net.URL中定义了URL相关的操作,其主要利用的是openStream();方法来返回一个InputStream,
 * 然后可以使用 InputStreamReader和BufferedReader来封装从而获取网上已发布的资源内容。
public class WebSpiderTest {
	// 将访问链接的操作封装成一个方法
	public static String getURLContent(String urlString) throws Exception {
		// 如果有一大推字符串要返回,当然是不可能每次取到就返回的,那么就将其放到一个字符缓冲区中,最后Stringbuilder.tostring(),以字符串的形式返回
		StringBuilder stringBuilder = new StringBuilder();
		try {
			// 必须要联网,否则是取不到结果的。其实这一步好比我们去读取文本地文件内容一样,只不过这里换成的是网络版本的“文件”
			//创建URL类对象
			URL url= new URL(urlString);;
			String temp = "";
			//通过openStream()方法返回一个inputstream输入流
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(url.openStream(),Charset.forName("gbk")));
			while ((temp = bufferedReader.readLine()) != null) {
				// System.out.println(temp);
				stringBuilder.append(temp);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		return stringBuilder.toString();
	public static void main(String[] args) throws Exception {
		// 不能再静态方法中引用非静态方法
		String string = getURLContent("http://www.sina.com");
		// System.out.println(string);
		// href=\"([\\w\\s./:]+?)\""  该方法匹配结果比下面更好,直接返回如下格式 http://m.sina.com.cn/m/weibo.shtml
		//(<a\\s*href=[^>]*>)
		Pattern pattern = Pattern.compile("href=\"([\\w\\s./:]+?)\"");
		Matcher matcher = pattern.matcher(string);
		boolean result = matcher.find();
		while (result) {
			for (int i = 1; i <= matcher.groupCount(); i++) {
				System.out.println(matcher.group(i));
//			System.out.println(matcher.group());
				result = matcher.find();
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
public class URLConnectionDemo {
	public static void main(String[] args) {
		try {
			URL url = new URL("http://www.sina.com");
			//打开一个URL连接,并运行客户端访问资源。
			URLConnection urlConnection = url.openConnection();
			HttpURLConnection connection = null;
			if (urlConnection instanceof HttpURLConnection) {
				connection = (HttpURLConnection) urlConnection;
			} else {
				System.out.println("Please enter an HTTP URL.");
				return;
			BufferedReader in = new BufferedReader(new InputStreamReader(
					connection.getInputStream(),Charset.forName("UTF-8")));
			String urlString = "";
			String current;
			while ((current = in.readLine()) != null) {
				urlString += current;
			System.out.println(urlString);
		} catch (IOException e) {
			e.printStackTrace();
                    protected String toExternalForm(URL u) {        // pre-compute length of StringBuffer        int len = u.getProtocol().length() + 1;        if (u.getAuthority() != null && u.getAuthority().length(
				
本文实例讲述了CI框架URI.php中_fetch_uri_string()函数用法。分享给大家供大家参考,具体如下: APPPATH/config/config.php中对于url 格式的拟定。 $config['uri_protocol'] = 'AUTO'; 这个配置项目定义了你使用哪个服务器全局变量来拟定URL。 默认的设置是auto,会把下列四个方式轮询一遍。当你的链接不能工作的时候,试着用用auto外的选项。 ‘AUTO’            Default – auto detects ‘PATH_INFO’        Uses the PATH_INFO
1 Map<String, Object> urlSplit(String data){ 2 StringBuffer strbuf = new StringBuffer(); 3 StringBuffer strbuf2 = new StringBuffer(); 4 Map<String ,Object> ...
需要提前了解下什么是URI,及URI和URL的区别: [URI、 URL 和 URN 的区别](http://www.jianshu.com/p/09ac6fc0f8cb)URI 引用包括最多三个部分:模式、模式特定部分和片段标识符。一般为: 模式:模式特定部分:片段 如果省略模式,这个URI引用则是相对的。如果省略片段标识符,这个URI引用就是一个纯URI。URI是对URL的抽象,不
注:在此用的JDK8的版本。 首先,URL在java.net这个包,签名如下:public final class URL implements java.io.Serializable可见URL是一个final,即URL无法被继承,并实现了Serializable接口,即URL对象可被序列化。 再看URL主要的实例属性: private String protocol; priva...
确定系统需求,包括商品管理的功能(如添加商品、编辑商品、删除商品、查看商品列表等)。 设计数据库模型,包括商品表、别表、库存表等。 确定系统的技术栈,如使用Spring MVC作为MVC框架、Hibernate或MyBatis作为ORM框架、Spring Security进行权限控制等。 环境搭建: 搭建开发环境,包括安装JDK、配置Servlet容器(如Tomcat)、配置数据库(如MySQL)等。 创建一个Maven项目,添加所需的依赖库。 数据库设计与创建: 根据设计好的数据库模型,在数据库中创建相应的表结构。 后端开发: 创建Java实体,对应数据库中的表结构。 编写数据访问层(DAO)代码,实现对商品信息的增删改查操作。 编写服务层(Service)代码,实现业务逻辑,如商品管理的各种操作。 开发控制器层(Controller),实现与前端页面的交互,接收请求并调用相应的服务进行处理。 前端开发: 使用HTML、CSS和JavaScript等前端技术,设计并实现商品管理页面的界面。 通过Ajax技术,实现前后端的数据交互,如异步加载商品列表、实
如果您想要在程序中输出月份的英文名称,并且提到了“pta”,但这里的“pta”与月份名称的输出没有直接关系(除非您是在特定的上下文中使用它作为一个变量名或标识符)。不过,我将直接给出如何输出月份英文名称的代码示例。 在Python中,您可以使用一个列表(list)或字典(dictionary)来存储月份的英文名称,并根据需要输出它们。以下是一个简单的示例: python # 使用列表存储月份的英文名称 months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] # 假设我们要输出第5个月份(即May) month_index = 4 # 注意列表索引从0开始,所以5月份是索引4 print(months[month_index]) # 输出: May # 或者,如果您想要通过月份的数字(1-12)来输出名称,可以稍作调整 def get_mo
Selenium是一个用于自动化浏览器操作的工具,它提供了一组API可以与各种浏览器进行交互。Selenium的源码解读可以帮助我们深入了解其内部实现原理和工作机制。 Selenium的源码主要由Java编写,整体结构分为三个部分:核心模块、浏览器驱动和客户端库。 1. 核心模块:核心模块包含了Selenium的核心功能,如元素定位、页面操作、JavaScript执行等。其中,最重要的是WebDriver接口,它定义了与浏览器交互的方法和属性。WebDriver接口的实现包括ChromeDriver、FirefoxDriver等,它们通过与浏览器驱动进行通信来实现对浏览器的控制。 2. 浏览器驱动:浏览器驱动是Selenium与各种浏览器进行交互的桥梁。每种浏览器都需要对应的驱动程序来实现与Selenium的通信。例如,ChromeDriver用于与Chrome浏览器进行交互,FirefoxDriver用于与Firefox浏览器进行交互。浏览器驱动负责启动浏览器进程、发送命令给浏览器、获取页面内容等操作。 3. 客户端库:Selenium支持多种编程语言,如Java、Python、C#等。每种语言都有对应的客户端库,用于在代码中调用Selenium的功能。客户端库提供了一组API,可以方便地进行元素定位、页面操作等操作。 如果你想深入了解Selenium的源码,可以从以下几个方面入手: 1. 研究WebDriver接口及其实现源码,了解与浏览器交互的具体实现方式。 2. 深入理解元素定位的原理和实现方式,包括XPath、CSS选择器等。 3. 学习浏览器驱动的源码,了解其启动浏览器进程、发送命令等操作。 4. 研究客户端库的源码,了解如何调用Selenium的功能。