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:
FileServer:
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;
}
}
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;
}
}