OReilly.Java.I.O.2nd.Edition
5.1. URLs
InputStream in = null;
try {
URL u = new URL("http://www.oreilly.com/");
in = u.openStream();
for (int c = in.read(); c != -1; c = in.read()) {
System.out.write(c);
}
} catch (MalformedURLException ex) {
} finally {
if (in != null)
in.close();
}
Most
network connections, even on LANs, are
slower and less reliable sources of data than files. Connections across the Internet are even
slower and less reliable, and connections through a modem are slower and less reliable still.
One way to enhance performance under these conditions is to buffer the data: to read as much data as you can into a temporary storage array inside the class, then parcel it out as needed. In the next chapter, you'll learn about the
BufferedInputStream class that does exactly this.
Untrusted code running
under the control of a security manager e.g.,
applets that run inside a web browser are normally allowed to connect
only to the host they were downloaded from. This host can be determined from the URL returned by the getCodeBase( ) method of the Applet class. Attempts to
connect to other hosts throw security exceptions. You can create URLs that point to other hosts, but you may not download data from them using openStream( ) or any other method. (This security restriction for applets applies to any network connection, regardless of how you get it.)
5.2. URL Connections
URL connections are closely related to URLs, as their name implies. Indeed, you get a reference to a URLConnection by using the openConnection( ) method of a URL object; in many ways, the URL class is only a wrapper around the URLConnection class. URL connections
provide more control over the communication between the client and the server.
分享到:
相关推荐
访问带https请求忽略ssl证书,避免url.openStream报错javax.net.ssl.SSLHandshakeException url = new URL(imageUrl);...DataInputStream dataInputStream = new DataInputStream(url.openStream());
本主题将深入探讨如何使用开源地图服务openstream来实现这一功能。 首先,openstream是一个免费开放的地图服务,它为开发者提供了一个灵活且可自定义的平台,用于在网页中展示地图信息。与商业地图服务相比,open...
google或者baidu一下,好多这样的问题,解决的方法都是修改php.ini,把allow_url_fopen给启用,改成 allow_url_fopen = On 这样做可以解决某些人的问题,有人说在php.ini中,有这样两个选项:allow_url_fopen =on...
`openStream()`方法同样属于`URL`类,它返回一个`InputStream`,通过这个输入流可以读取URL指向的数据。这种方法是最常用且灵活的,适合处理任何类型的数据流,如文本、图像或音频。只需创建一个URL对象并调用`open...
JAVA解决URL路径中含有中文的问题。无论是路径中还是文件名包含中文都可以处理。经测试验证通过。
在获取URL对象后,我们可以使用`openStream()`方法来打开一个连接并获取该URL指向的资源的输入流。这是下载网页内容的第一步: ```java import java.io.InputStream; import java.io.IOException; InputStream in ...
一旦创建了URL对象,就可以通过调用`URL`对象的`openStream()`方法来读取该URL所指向的信息。`openStream()`方法返回一个`InputStream`对象,可用于从URL中读取数据。 示例代码展示如何读取`www.sina.com.cn`的主页...
此外,`URL`类还提供了`openStream()`方法,可以打开到资源的连接,并返回一个`InputStream`,允许以字节流的方式读取资源内容。这在下载文件或获取网页内容时非常有用。 以下是一个简单的示例程序`URLDemo1`,它...
此段代码首先尝试使用指定的URL字符串创建一个`URL`对象,然后调用`openStream()`方法打开一个到该URL所引用资源的连接,并获取一个输入流。如果在这个过程中出现任何异常,则捕获这些异常并打印出错误信息,同时将`...
2. **打开连接并获取页面内容**:使用`openStream()`方法打开与URL的连接,并获取网页的HTML内容。这一步通常涉及到HTTP请求,但在这里我们假设已经完成了这个步骤,因为描述中提到的是直接修改URL。 3. **使用...
例如,通过`openConnection()`方法可以建立与URL所表示的资源的连接,`openStream()`则可以读取资源内容。 "工具"标签可能暗示了博客会介绍一些实用的URL处理工具或者编程技巧。在实际开发中,开发者经常需要处理...
DataInputStream dataInputStream = new DataInputStream(url.openStream()); ``` 接下来,可以逐行读取文本文件,并进行相应的处理。这通常涉及到读取一行,解析该行,然后执行所需的操作。例如: ```java ...
DataInputStream in = new DataInputStream(url.openStream()); String inputStream = null; inputStream = in.readLine(); while(inputStream!=null){ System.out.println(inputStream); inputStream ...
这个类提供了许多方法,如`openConnection()`用于打开到URL的连接,`openStream()`用于读取URL指向的资源,以及`toExternalForm()`用于将URL对象转换为字符串。 例如,如果我们有一个URL `...
比如,你可以使用`URL.openStream()`来获取网页的输入流,或者使用`URL.openConnection()`来建立一个`HttpURLConnection`,这为更复杂的网络操作提供了可能,如设置请求头、处理响应码等。 服务器端通信通常涉及到...
`openStream()`方法则可以打开一个到此URL引用的资源的连接,并返回一个输入流,以便读取该资源。 ### 输入流与输出流 - `BufferedInputStream`:这是Java提供的一个过滤输入流,它使用内部缓冲区来提高性能。在...
InputStream in = url.openStream(); ``` - **获取协议、主机和端口信息**:`URL`对象还提供了获取协议、主机名和端口号的方法。 - 示例代码: ```java String protocol = url.getProtocol(); String host = ...
response = requests.get(url, stream=True) if response.status_code == 200: with open('song1.mp3', 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) ``` 这段代码将从给定的URL下载...
在下载大文件时,我们会用到`URL.openStream()`方法来打开与服务器的连接,并获取输入流。 然后,我们引入`java.nio`包,这个包提供了非阻塞I/O操作,使得在多线程环境下处理数据更高效。特别是`java.nio.channels....
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 500 Read timed outin D:\website\extra.php on line 65”我在程序的开始已经有set_time_limit(0);了啊,那上面的错误会是因...