来自URL的InputStream [英] InputStream from a URL
本文介绍了来自URL的InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从URL获取InputStream?
How do I get an InputStream from a URL?
例如,我想在url wwww.somewebsite上获取该文件。 com / a.txt
并通过servlet将其作为Java中的InputStream读取。
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.
我试过
InputStream is = new FileInputStream("wwww.somewebsite.com/a.txt");
但我得到的是一个错误:
but what I got was an error:
java.io.FileNotFoundException
推荐答案
使用 java.net.URL#openStream()
,带有正确的URL(包括协议!)。例如
Use java.net.URL#openStream()
with a proper URL (including the protocol!). E.g.
InputStream input = new URL("http://www.somewebsite.com/a.txt").openStream();
// ...
参见:
- 使用java.net.URLConnection来触发和处理HTTP请求
- Using java.net.URLConnection to fire and handle HTTP requests
See also:
这篇关于来自URL的InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文