I have a static file upload method:
public class Downloader { private static MainLayoutController mlController; private static int doneQuantity = 0; public static void setController(MainLayoutController c) { mlController = c; } public static void downloadFile(String url, String savePath, int buffSize) { try { /* Get connection */ URL connection = new URL(url); HttpURLConnection urlconn; urlconn = (HttpURLConnection) connection.openConnection(); urlconn.setRequestMethod("GET"); urlconn.connect(); /* Set input stream */ InputStream in = null; in = urlconn.getInputStream(); /* Find file full path */ String[] tempArr = url.split("/"); String fullPath = savePath + tempArr[tempArr.length - 1]; /* Set Labels */ //mlController.downloadingLabel.setText(tempArr[tempArr.length - 1]); //mlController.downloadedLabel.setText(doneQuantity + "/" + mlController.quantity); /*Task task = new Task() { @Override protected Integer call() throws Exception { Platform.runLater(() -> updateTitle(tempArr[tempArr.length - 1])); return 101; } }; mlController.downloadingLabel.textProperty().bind(task.titleProperty()); new Thread(task).start();*/ /* Set write stream */ OutputStream writer = new FileOutputStream(fullPath); byte buffer[] = new byte[buffSize]; // Max bytes per one reception /* Download */ int i = 0; double getted_b = 0.0; long delta_t = System.nanoTime(), i_sum = 0; while ((i = in.read(buffer)) > 0) { getted_b += i; i_sum += i; writer.write(buffer, 0, i); if ((System.nanoTime() - delta_t) >= 1E9) { // If the second was over int kb, mb; mb = new Double(getted_b / (1024 * 1024)).intValue(); kb = new Double((getted_b / 1024) % 1024).intValue(); System.out.println(" >> Speed: " + mb + " " + kb + " Mb/sec"); System.out.println(" >> " + i_sum / (urlconn.getContentLength() / 100) + "%"); delta_t = System.nanoTime(); // Set to zero getted_b = 0.0; } } /* Cleaning */ writer.flush(); writer.close(); in.close(); } catch (IOException e) { System.out.println(e); } } }
He gets access to the label
to the progressbar
and progressbar
through a link to the mlController
controller mlController
. My unsuccessful attempts to update the labels come after the comment /* Set Labels */
. Tell me how best to do this?