如何在java中重置URLConnection?
有什么方法可以重置连接,以便我可以从文档的开头阅读? 重新连接从 sarting 读取文件是一个缓慢的过程,我想通过 http 每秒读取文件 20 次,还有其他方法可以做到这一点吗?
is there any way to reset the connection,so that i can read from starting of the document?
It is a slow process that reconnecting to read from sarting,i want to read a file 20 times per second through http,is there any other way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
URLConnection 是一个简单但有些“愚蠢”的类。我建议您采用 Java 方式:实例化一个新的 URLConnection。 ;)
Java 并不是真正鼓励对象重用的语言。
您可以尝试重置输入流,请参阅 docs 了解更多信息,但我认为它不会再进行 GET 操作。谁知道URLConnections的InputStream和OutputStream背后的具体实现是如何完成的。
编辑:我不保证任何事情,但您也可以尝试断开连接(),然后再次连接()。没试过,看看好不好用。
URLConnection is an easy but somewhat "dumb" class. I can suggest you to do it the Java way: instantiate a new URLConnection. ;)
Java is not really a language that encorages reusage of objects.
You can try resetting the input stream, see the docs for more info, but I don't think it will do another GET. Who knows how the concrete implementation behind InputStream and OutputStream of URLConnections is done.
EDIT: I don't guarantee anything, but you could also try to disconnect() and then connect() again. Never tried it, see if it works.
默认情况下没有办法。如果您确实想要此功能,您已经为此编写了一些代码。
编写一个 URLHandler,它提供一个套接字包装器,用于缓冲。根据需要实现重置方法()。
谢谢,
拉梅什
By default there is no way. If you really want this feature, you have write some code for that.
Write a URLHandler which gives a socket wrapper, that does buffering. Implement reset methods() as you like.
Thanks,
Ramesh