To do this yourself; you'll need to query a geo ip service with the IP address of the device to get its approximate location; then compare that with which server to best serve the file from. This has its own problems (proxy/vpn tunnels being one of them).
Second is to ping your server. The problem here could be that ping may be blocked by the provider (especially if the device is on not on wifi); even if they are on wifi, ping may be blocked for external addresses and only allowed within the network. In this case; the ping will always fail.
So the reliable way is to use a Content Delivery Network. They will take care of serving the file from the closest possible server automatically. You don't have to modify your application or manage different servers.