从 uri 打开流 [英] Open stream from uri

查看:35
本文介绍了从 uri 打开流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 uri (java.net.URI),例如 http://www.example.com.如何在 Java 中将其作为流打开?

I got an uri (java.net.URI) such as http://www.example.com. How do I open it as a stream in Java?

我真的必须改用 URL 类吗?

Do I really have to use the URL class instead?

推荐答案

您必须创建一个新的 URL 对象,然后在 URL 实例上打开流.下面是一个例子.

You will have to create a new URL object and then open stream on the URL instance. An example is below.

try {

   URL url = uri.toURL(); //get URL from your uri object
   InputStream stream = url.openStream();

} catch (MalformedURLException e) {
   e.printStackTrace();
} catch (URISyntaxException e) {
   e.printStackTrace();
}catch (IOException e) {
   e.printStackTrace();
}

这篇关于从 uri 打开流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆