Java文件输入流与URL一起使用


问题内容

如何在fileinputstream中输入要发送的文件?

我在Fileinputstream中输入了url,但是URL的输出是错误的,因为链接斜杠像-从/到\一样向后翻转,而双斜杠//只能是一个斜杠和一个反斜杠。要执行fileinputstream吗?如果不是,您能告诉我应该使用什么代替fileinputstream吗?


问题答案:

如果您要获取InputStream从URL检索数据的,则使用URL.openStream方法将返回InputStream,可以像使用其他方法一样使用InputStream

例如,

InputStream is;

// if we were getting data from a file, we might use:
is = new FileInputStream("/path/to/file");

// or, from a URL, then retrieve an InputStream from a URL
is = new URL("http://google.com/").openStream();