Add sending file
This commit is contained in:
parent
7462265f08
commit
4010f687ba
@ -1,6 +1,7 @@
|
||||
package ftp.sar;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
|
||||
public class FileChooser {
|
||||
|
||||
@ -14,4 +15,12 @@ public class FileChooser {
|
||||
return fileChooser.getSelectedFile().toString();
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
fileChooser.setDialogTitle("Choose file to send");
|
||||
fileChooser.showSaveDialog(null);
|
||||
|
||||
return fileChooser.getSelectedFile();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.FlowPane;
|
||||
import javafx.scene.layout.Pane;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.ResourceBundle;
|
||||
@ -21,6 +22,7 @@ public class ServerPanelController implements Initializable {
|
||||
private Server server;
|
||||
private FilePaneBuilder filePaneBuilder = new FilePaneBuilder();
|
||||
private MainPageController mainPageController;
|
||||
private FileChooser fileChooser = new FileChooser();
|
||||
|
||||
@FXML
|
||||
Pane serverPane;
|
||||
@ -49,6 +51,17 @@ public class ServerPanelController implements Initializable {
|
||||
this.mainPageController.serverContainer.getChildren().remove(this.serverPane);
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void uploadFile(Event e) {
|
||||
File file = this.fileChooser.getFile();
|
||||
|
||||
try {
|
||||
this.server.getConnector().sendFile(file);
|
||||
} finally {
|
||||
this.refreshList(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void setData(Server server, MainPageController mainPageController) {
|
||||
this.mainPageController = mainPageController;
|
||||
this.server = server;
|
||||
|
@ -124,4 +124,26 @@ public class ServerConnector {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void sendFile(File file) {
|
||||
try {
|
||||
this.sendRequest("SEND_" + file.getName());
|
||||
this.sendRequest(String.valueOf(file.length()));
|
||||
|
||||
byte[] fileBytes = new byte[(int) file.length()];
|
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
|
||||
bufferedInputStream.read(fileBytes, 0, fileBytes.length);
|
||||
|
||||
OutputStream outputStream = this.server.getSocket().getOutputStream();
|
||||
outputStream.write(fileBytes, 0, fileBytes.length);
|
||||
outputStream.flush();
|
||||
|
||||
} catch (CannotConnectException e) {
|
||||
e.printStackTrace();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@
|
||||
<?import javafx.scene.layout.FlowPane?>
|
||||
<?import javafx.scene.layout.Pane?>
|
||||
|
||||
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" fx:id="serverPane" prefWidth="780.0" style="border: black;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ftp.sar.controller.ServerPanelController">
|
||||
<Pane fx:id="serverPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="780.0" style="border: black;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ftp.sar.controller.ServerPanelController">
|
||||
<children>
|
||||
<Label layoutX="22.0" layoutY="-1.0" prefHeight="30.0" text="IP Addres: " />
|
||||
<Label fx:id="serverIp" layoutX="88.0" layoutY="5.0" text="xxx.xxx.xxx.xxx" />
|
||||
@ -14,5 +14,6 @@
|
||||
<FlowPane fx:id="fileContainer" layoutY="30.0" maxWidth="1.7976931348623157E308" minWidth="-Infinity" prefWidth="780.0" />
|
||||
<Button fx:id="refreshButton" layoutX="640.0" layoutY="0" mnemonicParsing="false" onAction="#refreshList" text="Refresh" />
|
||||
<Button fx:id="closeButton" layoutX="720.0" layoutY="0" mnemonicParsing="false" onAction="#closeConnection" text="Close" />
|
||||
<Button layoutX="568.0" mnemonicParsing="false" onAction="#uploadFile" text="Upload" />
|
||||
</children>
|
||||
</Pane>
|
||||
|
Loading…
Reference in New Issue
Block a user