InputStream from a URL

后端 未结 6 391
死守一世寂寞
死守一世寂寞 2020-12-02 14:41

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

6条回答
  •  情书的邮戳
    2020-12-02 15:44

    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.

提交回复
热议问题