来自 URL 的输入流
如何从 URL 获取 InputStream?
例如,我想获取 URL www.somewebsite.com/a.txt
处的文件,并通过 servlet 将其作为 Java 中的 InputStream 读取。
我已经尝试过
InputStream is = new FileInputStream("wwww.somewebsite.com/a.txt");
,但得到的是一个错误:
java.io.FileNotFoundException
How do I get an InputStream from a URL?
for example, I want to take the file at the url wwww.somewebsite.com/a.txt
and read it as an InputStream in Java, through a servlet.
I've tried
InputStream is = new FileInputStream("wwww.somewebsite.com/a.txt");
but what I got was an error:
java.io.FileNotFoundException
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
java.net .URL#openStream()
具有正确的 URL(包括协议!)。例如,另请参阅:
Use
java.net.URL#openStream()
with a proper URL (including the protocol!). E.g.See also:
尝试:
Try:
(a)
www.somewebsite.com/a.txt
不是“文件 URL”。它根本不是一个 URL。如果您将http://
放在其前面,它将是一个 HTTP URL,这显然就是您的意图。(b)
FileInputStream
用于文件,而不是 URL。(c) 从任何 URL 获取输入流的方法是通过
URL.openStream(),
或URL.getConnection().getInputStream(),< /code> 这是等效的,但您可能有其他原因来获取
URLConnection
并首先使用它。(a)
wwww.somewebsite.com/a.txt
isn't a 'file URL'. It isn't a URL at all. If you puthttp://
on the front of it it would be an HTTP URL, which is clearly what you intend here.(b)
FileInputStream
is for files, not URLs.(c) The way to get an input stream from any URL is via
URL.openStream(),
orURL.getConnection().getInputStream(),
which is equivalent but you might have other reasons to get theURLConnection
and play with it first.纯Java:
我使用这种方法取得了一些成功。它处理重定向,并且可以将可变数量的HTTP 标头作为
Map
传递。它还允许从 HTTP 重定向到 HTTPS。完整的调用示例
Pure Java:
With some success I use this method. It handles redirects and one can pass a variable number of HTTP headers as
Map<String,String>
. It also allows redirects from HTTP to HTTPS.Full example call
您的原始代码使用 FileInputStream,它用于访问文件系统托管文件。
您使用的构造函数将尝试在当前工作目录(系统属性 user.dir 的值)的 www.somewebsite.com 子文件夹中查找名为 a.txt 的文件。您提供的名称将使用 File 类解析为文件。
URL 对象是解决这个问题的通用方法。您可以使用 URL 来访问本地文件,也可以使用网络托管资源。除了 http:// 或 https:// 之外,URL 类还支持 file:// 协议,因此您可以开始使用。
Your original code uses FileInputStream, which is for accessing file system hosted files.
The constructor you used will attempt to locate a file named a.txt in the www.somewebsite.com subfolder of the current working directory (the value of system property user.dir). The name you provide is resolved to a file using the File class.
URL objects are the generic way to solve this. You can use URLs to access local files but also network hosted resources. The URL class supports the file:// protocol besides http:// or https:// so you're good to go.
InputStream input = URI.create("http://www.somewebsite.com/a.txt").toURL().openStream();
增强whiskeysierra 的答案,
java.net.URL.URL(String)
构造函数是已弃用Java 20。InputStream input = URI.create("http://www.somewebsite.com/a.txt").toURL().openStream();
Enhancing whiskeysierra's answer, the
java.net.URL.URL(String)
constructor is deprecated since Java 20.这是读取给定网页内容的完整示例。
该网页是从 HTML 表单中读取的。我们使用标准
InputStream
类,但使用 JSoup 库可以更轻松地完成。这些是 Maven 依赖项。我们使用 Apache Commons 库来验证 URL 字符串。
ReadWebPage
servlet 读取给定网页的内容并将其以纯文本格式发送回客户端。读取页面的任务被委托给WebPageReader
。WebPageReader
验证 URL 并读取网页内容。它返回一个包含页面 HTML 代码的字符串。
最后,这是包含 HTML 表单的主页。
这是摘自我关于此主题的教程。
Here is a full example which reads the contents of the given web page.
The web page is read from an HTML form. We use standard
InputStream
classes, but it could be done more easily with JSoup library.These are the Maven dependencies. We use Apache Commons library to validate URL strings.
The
ReadWebPage
servlet reads the contents of the given web page and sends it back to the client in plain text format. The task of reading the page is delegated toWebPageReader
.WebPageReader
validates the URL and reads the contents of the web page.It returns a string containing the HTML code of the page.
Finally, this is the home page containing the HTML form.
This is taken from my tutorial about this topic.