How do you detect a network disconnection when downloading a file in java?

后端 未结 2 1631
花落未央
花落未央 2021-01-07 07:01

I am using the following code to download a file using Java but i want to detect when connection is lost. I ran the following code and while in the middle of downloading i d

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-07 07:35

    Since you are using HTTP, you would be best of using the Apache HttpClient project. org.apache.http.client.config.RequestConfig.Builder allows to set various timeout values that have no correspondence in java.net.URL and related classes. Especially setting a socket timeout would help in your case. You can then use org.apache.http.HttpEntity.writeTo to directly write the resource to a FileOutputStream.

提交回复
热议问题