Wie binde ich eine JavaFX ProgressBar an eine Datei Übertragung?

michadaniel

Grünschnabel
Hallo,
Ich habe einen FileClient und einen FileServer gemacht, weiß aber nicht, wie ich den Fortschritt der Datei Übertragung an die ProgressBar binden kann.
FileClient:
Java:
public class FileClient extends Application implements FileTransfer {
    public static final int PORT = 7070;
    public static final String ADDRESS = "michadaniel4.ddns.net";

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox();
        Button button = new Button("Choose your File...");
        button.setPrefWidth(400);
        button.setPrefHeight(50);
        ProgressBar progressBar = new ProgressBar();
        progressBar.setPrefWidth(400);
        progressBar.setPrefHeight(50);
        root.getChildren().addAll(button, progressBar);
        Scene scene = new Scene(root, 400, 100);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.sizeToScene();
        primaryStage.setTitle("File Client");
        primaryStage.show();

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                File file = new FileChooser().showOpenDialog(null).getAbsoluteFile();
                Task task = new Task() {
                    @Override
                    protected Object call() throws Exception {
                        try {
                            Socket socket = new Socket(ADDRESS, PORT);
                            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
                            byte[] bytes = readData(file.getAbsolutePath());
                            dataOutputStream.writeInt(bytes.length);
                            dataOutputStream.write(bytes);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                };
                new Thread(task).start();
            }
        });
    }

    @Override
    public boolean writeData(byte[] data, String fileName) {
        File file = new File(fileName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(data);
            fileOutputStream.flush();
            return true;
        } catch (FileNotFoundException e) {
            System.err.println(file + " doesn't exist!");
        } catch (IOException e) {
            System.err.println("Problems writing data to " + file);
        } finally {
            try {
                if (fileOutputStream != null) fileOutputStream.close();
            } catch (IOException e) {
                System.err.println("FileOutputStream cannot be closed");
            }
        }
        return false;
    }

    @Override
    public byte[] readData(String fileName) {
        File file = new File(fileName);
        FileInputStream fileInputStream = null;
        byte[] data = null;
        try {
            fileInputStream = new FileInputStream(file);
            data = new byte[(int) file.length()];
            fileInputStream.read(data);
        } catch (FileNotFoundException e) {
            System.err.println(file + " doesn't exist!");
        } catch (IOException e) {
            System.err.println("Problems reading data from " + file);
        } finally {
            try {
                if (fileInputStream != null) fileInputStream.close();
            } catch (IOException e) {
                System.err.println("FileInputStream cannot be closed");
            }
        }
        return data;
    }
}
FileServer:
Java:
public class FileServer implements FileTransfer {
    public static final int PORT = 7070;

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(PORT);
            while (true) {
                Socket socket = serverSocket.accept();
                DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
                long start = System.currentTimeMillis();
                int length = dataInputStream.readInt();
                if (length > 0) {
                    byte[] bytes = new byte[length];
                    dataInputStream.readFully(bytes);
                    new FileServer().writeData(bytes, "C:\\Users\\Michadaniel\\IdeaProjects\\FileTransfer\\in\\test.jar");
                }
                long stop = System.currentTimeMillis();
                double time = (stop - start) / 1000;
                System.out.println("Time: " + time + "s");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean writeData(byte[] data, String fileName) {
        File file = new File(fileName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(data);
            fileOutputStream.flush();
            return true;
        } catch (FileNotFoundException e) {
            System.err.println(file + " doesn't exist!");
        } catch (IOException e) {
            System.err.println("Problems writing data to " + file);
        } finally {
            try {
                if (fileOutputStream != null) fileOutputStream.close();
            } catch (IOException e) {
                System.err.println("FileOutputStream cannot be closed");
            }
        }
        return false;
    }

    @Override
    public byte[] readData(String fileName) {
        File file = new File(fileName);
        FileInputStream fileInputStream = null;
        byte[] data = null;
        try {
            fileInputStream = new FileInputStream(file);
            data = new byte[(int) file.length()];
            fileInputStream.read(data);
        } catch (FileNotFoundException e) {
            System.err.println(file + " doesn't exist!");
        } catch (IOException e) {
            System.err.println("Problems reading data from " + file);
        } finally {
            try {
                if (fileInputStream != null) fileInputStream.close();
            } catch (IOException e) {
                System.err.println("FileInputStream cannot be closed");
            }
        }
        return data;
    }
}
 
Ok, es kam zwar keine Antwort, aber ich habe es inzwischen zu einer selbstständigen Lösung gebracht:D.
Also ist das Thema erledigt.

FileClient:
Java:
package net.ddns.michadaniel4.filetransfer;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.*;
import java.net.Socket;

/**
* Created by Michadaniel on 14.03.2016.
*/
public class FileClient extends Application implements FileTransfer {
    public static final int PORT = 7070;
    public static final String ADDRESS = "michadaniel4.ddns.net";

    public ProgressBar progressBar = new ProgressBar();

    public Task task = null;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        VBox root = new VBox();
        Button button = new Button("Choose your File...");
        button.setPrefWidth(400);
        button.setPrefHeight(50);
        progressBar.setPrefWidth(400);
        progressBar.setPrefHeight(50);
        root.getChildren().addAll(button, progressBar);
        Scene scene = new Scene(root, 400, 100);
        primaryStage.setScene(scene);
        primaryStage.setResizable(false);
        primaryStage.sizeToScene();
        primaryStage.setTitle("File Client");
        primaryStage.show();

        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                File file = new FileChooser().showOpenDialog(null).getAbsoluteFile();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Socket socket = new Socket(ADDRESS, PORT);
                            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()) {
                                @Override
                                public void write(byte b[], int off, int len) throws IOException {
                                    if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
                                        throw new IndexOutOfBoundsException();

                                    task = new Task() {
                                        @Override
                                        protected Object call() throws Exception {
                                            for (int i = 0; i < len; i++) {
                                                write(b[off + i]);
                                                updateProgress(i, len);
                                            }
                                            return null;
                                        }
                                    };
                                    Platform.runLater(new Runnable() {
                                        @Override
                                        public void run() {
                                            progressBar.progressProperty().bind(task.progressProperty());
                                        }
                                    });
                                    task.run();
                                }
                            };
                            byte[] bytes = readData(file.getAbsolutePath());
                            dataOutputStream.writeUTF(file.getName());
                            dataOutputStream.writeInt(bytes.length);
                            dataOutputStream.write(bytes);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }

    @Override
    public boolean writeData(byte[] data, String fileName) {
        File file = new File(fileName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(data);
            fileOutputStream.flush();
            return true;
        } catch (FileNotFoundException e) {
            System.err.println(file + " doesn't exist!");
        } catch (IOException e) {
            System.err.println("Problems writing data to " + file);
        } finally {
            try {
                if (fileOutputStream != null) fileOutputStream.close();
            } catch (IOException e) {
                System.err.println("FileOutputStream cannot be closed");
            }
        }
        return false;
    }

    @Override
    public byte[] readData(String fileName) {
        File file = new File(fileName);
        FileInputStream fileInputStream = null;
        byte[] data = null;
        try {
            fileInputStream = new FileInputStream(file);
            data = new byte[(int) file.length()];
            fileInputStream.read(data);
        } catch (FileNotFoundException e) {
            System.err.println(file + " doesn't exist!");
        } catch (IOException e) {
            System.err.println("Problems reading data from " + file);
        } finally {
            try {
                if (fileInputStream != null) fileInputStream.close();
            } catch (IOException e) {
                System.err.println("FileInputStream cannot be closed");
            }
        }
        return data;
    }
}
 
Zurück