From 7403afc270daccda2064bd4d59bc4fc464a31298 Mon Sep 17 00:00:00 2001 From: moamdavoodi <58753725+moamdavoodi@users.noreply.github.com> Date: Fri, 24 Jul 2020 23:56:53 +0430 Subject: [PATCH] phase 3; no, i am the one who knocks! --- src/main/java/server/FileDownloadClient.java | 38 +++++++++ src/main/java/server/FileServer.java | 82 ++++++++++++++++++++ src/main/java/server/FileUploadClient.java | 38 +++++++++ 3 files changed, 158 insertions(+) create mode 100644 src/main/java/server/FileDownloadClient.java create mode 100644 src/main/java/server/FileServer.java create mode 100644 src/main/java/server/FileUploadClient.java diff --git a/src/main/java/server/FileDownloadClient.java b/src/main/java/server/FileDownloadClient.java new file mode 100644 index 00000000..f87470c7 --- /dev/null +++ b/src/main/java/server/FileDownloadClient.java @@ -0,0 +1,38 @@ +package server; + +import java.io.*; +import java.net.Socket; +import java.util.Scanner; + +public class FileDownloadClient { + private static final int PORT = 9060; + private static final String IP = "127.0.0.1"; + static DataInputStream in; + static DataOutputStream out; + static Scanner scanner; + private static Socket socket; + + public static void main(String[] args) { + scanner = new Scanner(System.in); + String name = scanner.nextLine(); + String path = scanner.nextLine(); + try { + socket = new Socket(IP, PORT); + in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); + out.writeUTF("DownloadName:" + name); + out.flush(); + int byteNumber = (int) Double.parseDouble(in.readUTF()); + byte[] allBytes = new byte[byteNumber]; + for (int i = 0; i < byteNumber; i++) { + allBytes[i] = in.readByte(); + } + FileOutputStream fileOutputStream = new FileOutputStream(path); + fileOutputStream.write(allBytes); + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } +} diff --git a/src/main/java/server/FileServer.java b/src/main/java/server/FileServer.java new file mode 100644 index 00000000..2175ab96 --- /dev/null +++ b/src/main/java/server/FileServer.java @@ -0,0 +1,82 @@ +package server; + +import java.io.*; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.file.Files; + +public class FileServer { + public static final int PORT = 9060; + private static final String PATH = "src\\main\\resources\\"; + private DataOutputStream out; + private DataInputStream in; + private ServerSocket fileServer; + + public FileServer() throws IOException { + fileServer = new ServerSocket(PORT); + new Thread(new Runnable() { + @Override + public void run() { + while (true) { + try { + Socket socket = fileServer.accept(); + in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); + new main.java.FileServer.ClientHandler(out, in, socket).start(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + }).start(); + } + + static class ClientHandler extends Thread { + private DataOutputStream dataOutputStream; + private DataInputStream dataInputStream; + private Socket socket; + + private ClientHandler(DataOutputStream dataOutputStream, DataInputStream dataInputStream, Socket socket) { + this.dataOutputStream = dataOutputStream; + this.dataInputStream = dataInputStream; + this.socket = socket; + } + + @Override + public void run() { + try { + String input; + while (true) { + input = dataInputStream.readUTF(); + if (input.startsWith("DownloadName:")) { + int colonIndex = input.indexOf(":"); + String fileName = input.substring(colonIndex + 1); + File file = new File(PATH + fileName); + byte[] fileContent = Files.readAllBytes(file.toPath()); + dataOutputStream.writeUTF("" + fileContent.length); + dataOutputStream.flush(); + for (byte b : fileContent) { + dataOutputStream.write(b); + dataOutputStream.flush(); + } + return; + } else if (input.startsWith("UploadName:")) { + String[] parts = input.split(":"); + String fileName = parts[1]; + int fileByteSize = Integer.parseInt(parts[2]); + byte[] allBytes = new byte[fileByteSize]; + for (int i = 0; i < fileByteSize; i++) { + allBytes[i] = dataInputStream.readByte(); + } + FileOutputStream fileOutputStream = new FileOutputStream(PATH + fileName); + fileOutputStream.write(allBytes); + fileOutputStream.close(); + return; + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} diff --git a/src/main/java/server/FileUploadClient.java b/src/main/java/server/FileUploadClient.java new file mode 100644 index 00000000..f8cfe4ae --- /dev/null +++ b/src/main/java/server/FileUploadClient.java @@ -0,0 +1,38 @@ +package server; + +import java.io.*; +import java.net.Socket; +import java.nio.file.Files; +import java.util.Scanner; + +public class FileUploadClient { + private static final int PORT = 9060; + private static final String IP = "127.0.0.1"; + static DataInputStream in; + static DataOutputStream out; + static Scanner scanner; + private static Socket socket; + + + public static void main(String[] args) { + scanner = new Scanner(System.in); + String name = scanner.nextLine(); + String location = scanner.nextLine(); + try { + socket = new Socket(IP, PORT); + in = new DataInputStream(new BufferedInputStream(socket.getInputStream())); + out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); + File file = new File(location + name); + byte[] fileContent = Files.readAllBytes(file.toPath()); + out.writeUTF("UploadName:" + name + ":" + fileContent.length); + out.flush(); + for (byte b : fileContent) { + out.write(b); + out.flush(); + } + } catch (IOException e) { + e.printStackTrace(); + } + + } +}