从 InputStream 获取 URL
Get URL from InputStream
我想知道 java 中的 InputStream 是如何从底层角度实现的。
假设我写了下面的 java 代码来连接网站。
url = new URL("[some url info]");
URLConnection urlcon = url.openConnection();
InputStream in = urlcon.getInputStream();
while((readcount = in.read(buffer)) != -1){
fos.write(buffer,0,readcount);
我可以通过强制转换它的类型并调用如下所示的适当方法直接从 InputStream(上面代码块中的 "in")知道 URL 吗?还有其他方法可以从 InputStream 获取 URL 吗?
(newtype) new = (newtype) in;
String Url = new.appropriatemethod();
我搜索了 InputStream 的所有子 类,但我找不到任何 类 有接口给它 URL。
(https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html)
但是,我认为 InputStream 以某种方式具有 URL 的信息,可以从具有此 URL.
的网站接收数据
我可能对"Stream"有很大的误解。
感谢您阅读我的问题。 :)
InputStream
只是 stream 而不是 URLConnection
。当您键入 InputStream in = urlcon.getInputStream();
时。您将获得输入流而不是 url 连接。当您键入 in.read
时,您正在阅读 Stream 而不是 URLConnection
.
An InputStream is a reference to source of data (be it a file, network
connection etc), that we want to process as follows:
we generally want to read the data as "raw bytes" and then write our own code to do something interesting with the bytes;
we generally want to read the data in sequential order: that is, to get to the nth byte of data, we have to read all the preceding bytes first, and we're not guaranteed to be able to "jump back" again once we've read them.
我想知道 java 中的 InputStream 是如何从底层角度实现的。
假设我写了下面的 java 代码来连接网站。
url = new URL("[some url info]");
URLConnection urlcon = url.openConnection();
InputStream in = urlcon.getInputStream();
while((readcount = in.read(buffer)) != -1){
fos.write(buffer,0,readcount);
我可以通过强制转换它的类型并调用如下所示的适当方法直接从 InputStream(上面代码块中的 "in")知道 URL 吗?还有其他方法可以从 InputStream 获取 URL 吗?
(newtype) new = (newtype) in;
String Url = new.appropriatemethod();
我搜索了 InputStream 的所有子 类,但我找不到任何 类 有接口给它 URL。 (https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html)
但是,我认为 InputStream 以某种方式具有 URL 的信息,可以从具有此 URL.
的网站接收数据我可能对"Stream"有很大的误解。
感谢您阅读我的问题。 :)
InputStream
只是 stream 而不是 URLConnection
。当您键入 InputStream in = urlcon.getInputStream();
时。您将获得输入流而不是 url 连接。当您键入 in.read
时,您正在阅读 Stream 而不是 URLConnection
.
An InputStream is a reference to source of data (be it a file, network connection etc), that we want to process as follows:
we generally want to read the data as "raw bytes" and then write our own code to do something interesting with the bytes;
we generally want to read the data in sequential order: that is, to get to the nth byte of data, we have to read all the preceding bytes first, and we're not guaranteed to be able to "jump back" again once we've read them.