Downloads the url to the destination file (with support for redirects).
(String url, File dst)
| 55 | public class FileMisc { |
| 56 | /** Downloads the url to the destination file (with support for redirects). */ |
| 57 | public static void download(String url, File dst) throws IOException { |
| 58 | mkdirs(dst.getParentFile()); |
| 59 | OkHttpClient client = new OkHttpClient.Builder().build(); |
| 60 | Request req = new Request.Builder().url(url).build(); |
| 61 | try (Response response = client.newCall(req).execute(); |
| 62 | BufferedSink sink = Okio.buffer(Okio.sink(dst))) { |
| 63 | try (ResponseBody body = response.body()) { |
| 64 | if (body == null) { |
| 65 | throw new IllegalArgumentException("Body was expected to be non-null"); |
| 66 | } |
| 67 | sink.writeAll(body.source()); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /////////////////////////////////////////////////////////////////// |
| 73 | // Replacements for File.* which check exceptional return values // |