有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

openurlconnection与connect之间的java差异?

我正在使用HttpURLConnection包下载一个大文件。我可以这样做,但我有点混淆了HttpURLConnection和HttpURLConnection。连接vs URL。openurlconnection方法

URL。openurlconnection返回HttpURLConnection。那么我可以重用这个对象吗?如果网络断开连接,然后重新连接,我想继续下载该文件。我可以直接调用connect()还是必须重新配置连接

链接到文档: HttpURLConnection:http://developer.安卓.com/reference/java/net/HttpURLConnection.html 网址:http://developer.安卓.com/reference/java/net/URL.html

谢谢


共 (1) 个答案

  1. # 1 楼答案

    Difference between openurlconnection vs connect?

    它们是等价的

    The URL.openurlconnection returns HttpURLConnection. So can I reuse this object ? If the network disconnects and then connects back, I would like to continue downloading the file. Can I directly call connect() or do I have to reconfigure the connection ?

    HttpURLConnection实例用于发出单个请求。它不能重复使用。如果要在失败后继续下载文件,则需要创建新连接

    如果要避免再次传输先前获取的文件部分,可以使用"Range Retrieval Request"。但是请注意,服务器端对范围检索的支持是可选的,因此客户端需要密切关注响应头,以确定它是获取整个文件还是仅获取请求的范围

    相关问题: