java.net.URL类

付费节点推荐


免费节点


节点使用教程


URl(Uniform Resource Locator)类代表统一资源定位器,统一资源定位器是指互联网“资源”的名称。资源可以是简单的文件或目录,也可以是对更为复杂的对 象的引用,例如对数据可或搜索引擎的查询。通常URL可以由协议名、主机、端口和资源组成。URL的格式为”protocol://host:port/resourceName”。例如,URL地
址“http://www.bcoder.cn”。

URL的构造方法

public URL (String str)throws MalformedURLException
该构造方法使用字符串初始化一个URL对象。
例如:

try {
URL url=new URL(“http://baidu.com”);
} catch (MalformedURLException e) {
e.printStackTrace();
}

URL常用方法

package cn.bcoder.net_Demo;

import java.net.MalformedURLException;
import java.net.URL;

public class URL_method {

public static void main(String[] args) {
URL url = null;
try {
url = new URL("http://baidu.com");
} catch (MalformedURLException e) {

e.printStackTrace();
}
System.out.println(url.getHost()); // 获取URL主机号
System.out.println(url.getFile()); // 获取URL文件名
System.out.println(url.getPort()); // 获取URL端口号
System.out.println(url.getProtocol()); // 获取URL协议
System.out.println(url.getQuery()); // 获取URL的查询部分
System.out.println(url.getUserInfo()); // 获取URL的用户信息

}

}

读取 URL 中的资源

URL对象调用InputStream openStream() 方法可以返回一个输入流,该输入流指向URL对象包含的资源。通过该输入流可以将服务器上的资源信息读入到客户端。


package cn.bcoder.net_Demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class URL_Resource {
public static void main(String[] args) throws IOException {

URL url;
url = new URL("http://bcoder.clbug.com");
// 通过URL的openStream方法获得URL对象所标识的资源的字节输入流。
InputStream is = url.openStream();
// 将字节输入流转换为字符输入流,并指定字符编码
InputStreamReader isr = new InputStreamReader(is, "utf-8");
// 为字符输入流提供缓冲流,提高I/O性能
BufferedReader bfr = new BufferedReader(isr);

String len;
while ((len = bfr.readLine()) != null) {
System.out.println(len);

}

bfr.close();
isr.close();
is.close();

}
}

下例中,将URL获取的资源通过FileOutputStream写入到指定文件中。由于网络速度或其他因素,URL资源可能引起堵塞,因此例子中使用了一个线程读取URL资源,以免堵塞主线程。

package cn.bcoder.net_Demo;

import java.net.URL;
import java.util.Scanner;

public class URL_Demo {

public static void main(String[] args) {
Scanner scanner;
URL url;
Thread readURL;
Look look = new Look();
System.out.println("输入url资源,例如:http://bcoder.clbug.com");
scanner = new Scanner(System.in);
String source = scanner.nextLine();
scanner.close();
try {
url = new URL(source);
look.setURL(url);
readURL = new Thread(look);
readURL.start();
}

catch (Exception exp) {
System.out.println(exp);

}

}

}

package cn.bcoder.net_Demo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class Look implements Runnable {
URL url;

public void setURL(URL url) {
this.url = url;
}

public void run() {
try {
InputStream is = url.openStream();
FileOutputStream writefile = new FileOutputStream("D://URL.html");

byte[] b = new byte[10000];
while (is.read(b) != -1) {
writefile.write(b);
}
writefile.close();

} catch (IOException e) {
}
System.out.println("url资源已保存至D://URL.html");
}
}

未经允许不得转载:Bcoder资源网 » java.net.URL类

相关推荐

更多优质资源关注微信公众号: bcoder

bcoder
赞 (0)
分享到:更多 ()

评论 0

评论前必须登录!

登陆 注册