/** * Downloads a remote file and puts it inside a given directory. Returns the path of the file * * @param url The URL * @param directory The local directory * @param headers Extra headers (optional) * @param onChange consumer to show the download progress (e.g. a progress bar) * @return The local file */ private File getInsideDirectory(URL url, File directory, Map<String, String> headers, Consumer<ProgressEntity> onChange) { try { PhoenicisUrlConnection connection = PhoenicisUrlConnection.fromURL(url); if (headers != null) { connection.setHeaders(headers); } final String fileName = connection.fetchFileName(); final File localFile = new File(directory, fileName); try (FileOutputStream fileOutputStream = new FileOutputStream( localFile)) { saveConnectionToStream(url, connection, fileOutputStream, onChange); return new File(directory, fileName); } } catch (IOException e) { throw new DownloadException(String.format(EXCEPTION_ITEM_DOWNLOAD_FAILED, url), e); } }
/** * downloads url to outputStream, shows progress via onChange * * @param url download URL * @param outputStream file is downloaded to this stream * @param headers HTTP headers to append * @param onChange consumer to show the download progress (e.g. a progress bar) */ private void get(URL url, OutputStream outputStream, Map<String, String> headers, Consumer<ProgressEntity> onChange) { try { PhoenicisUrlConnection connection = PhoenicisUrlConnection.fromURL(url); if (headers != null) { connection.setHeaders(headers); } connection.connect(); saveConnectionToStream(url, connection, outputStream, onChange); } catch (IOException e) { throw new DownloadException(String.format(EXCEPTION_ITEM_DOWNLOAD_FAILED, url), e); } }