From d0a9961b159ff986fedfcf83974d0ee984353775 Mon Sep 17 00:00:00 2001 From: pcheape Date: Sun, 8 Oct 2017 22:34:06 +0100 Subject: [PATCH] added functionality so that the files would be saved under filename rather than new by defining my own protocol --- Client.java | 62 ++++++++++++++++++++++++++++++++++++----------------- Server.java | 45 ++++++++++++++------------------------ 2 files changed, 58 insertions(+), 49 deletions(-) diff --git a/Client.java b/Client.java index a06b970..5dfeaa9 100644 --- a/Client.java +++ b/Client.java @@ -1,7 +1,9 @@ import java.net.Socket; +import java.nio.file.Files; import java.io.BufferedOutputStream; +import java.io.DataOutputStream; +import java.io.File; import java.io.BufferedInputStream; -import java.io.FileInputStream; import java.io.IOException; import java.util.Scanner; import javax.swing.JFileChooser; @@ -21,23 +23,24 @@ public static void main(String args[]) { System.out.print("Enter The Server IP Address: "); String ip = kb.nextLine(); + + try { + socket = new Socket(ip, port); - try { - socket = new Socket(ip, port); - - String file = getTransferFile(); - setUpStreams(file); - transferData(); - closeCrap(); - } catch(IOException ioe) { - ioe.printStackTrace(); - } + String file = getTransferFile(); + String fileName = getFileName(file); + setUpStreams(fileName,file); + transferData(); + closeCrap(); + } catch (IOException ioe) { + ioe.printStackTrace(); + } } /* * - * The getTransferFile() method takes the URL of the file to be transfered - * as String and then returns the URL as a String. + * The getTransferFile() method takes the URL of the file to be transfered as + * String and then returns the URL as a String. * */ private static String getTransferFile() { @@ -45,13 +48,23 @@ private static String getTransferFile() { JFileChooser fc = new JFileChooser(); int ans = fc.showOpenDialog(null); - if(ans == JFileChooser.APPROVE_OPTION) - { + if (ans == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile().getAbsolutePath(); + } return file; } + + //The getfileNameMethod gets us the filename so we can use it in the setUpStreams to send over the filename first . + + private static String getFileName(String filePath) { + File file = new File(filePath); + String fileName; + fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\") + 1); + System.out.println(fileName); + return fileName; + } /* * @@ -59,14 +72,23 @@ private static String getTransferFile() { * BufferedOutputStream output. The p_input takes a FileInputStream pointing * */ - private static void setUpStreams(String file) throws IOException { - p_input = new BufferedInputStream(new FileInputStream(file), buffer); - output = new BufferedOutputStream(socket.getOutputStream(), buffer); + private static void setUpStreams(String fileName,String path) throws IOException { + + File file = new File(path); + output = new BufferedOutputStream(socket.getOutputStream()); + try (DataOutputStream d = new DataOutputStream(output)) { + d.writeUTF(fileName); + Files.copy(file.toPath(), d); + } + + + } + private static void transferData() throws IOException { - while((n = p_input.read(b, 0, buffer)) != -1) { - output.write(b, 0, n); + while ((n = p_input.read(b, 0, buffer)) != -1) { + output.write(b, 0, n); } } diff --git a/Server.java b/Server.java index 8af7519..a89891c 100644 --- a/Server.java +++ b/Server.java @@ -1,7 +1,8 @@ import java.net.ServerSocket; import java.net.Socket; -import java.io.BufferedOutputStream; -import java.io.FileOutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.io.DataInputStream; import java.io.BufferedInputStream; import java.io.IOException; import java.io.EOFException; @@ -12,10 +13,9 @@ public class Server { private static int buffer = 16000000; private static ServerSocket server_socket; private static Socket socket; - private static BufferedOutputStream p_output; private static BufferedInputStream input; - private static byte[] b = new byte[buffer]; - private static int n; + + /* * @@ -34,7 +34,6 @@ public static void main(String args[]) { while(true) { waitForConnection(); setupStreams(); - transferData(); } } catch(EOFException eofe) { System.out.println("Error: " + eofe); @@ -65,6 +64,7 @@ private static void waitForConnection() throws IOException { System.out.println("Connected to " + host_name); } + /* * * Sets up the output and input streams of the server. @@ -78,38 +78,26 @@ private static void waitForConnection() throws IOException { * The flush() method flushes the output stream thus clearing * the output buffer in case there is some data left in the * buffer. There is no flush method for the input stream. + * + * I have changed this to define our own protocol using DataInput streams + * the client sends the file name and then the file we simply use Files.copy to copy this to the selected + * directory. saving a lot of code. * */ private static void setupStreams() throws IOException { System.out.println("Setting output stream..."); String dir = getDirectory(); - p_output = new BufferedOutputStream(new FileOutputStream(dir + "/new"), buffer); - System.out.println("Output stream set\nSetting up input stream..."); input = new BufferedInputStream(socket.getInputStream(), buffer); + try (DataInputStream d = new DataInputStream(input)) { + String fileName = d.readUTF(); + System.out.println("Copying File "+fileName ); + Files.copy(d,Paths.get(dir, fileName)); + } System.out.println("Input stream set"); } - /* - * - * The read() method takes data from BufferedInputStream input and writes - * it to the byte array b from element 0 to 16000000-1. The number of bytes - * read are stored in n. - * - * The write() method writes data from b to BufferedOutputStream output - * from position 0 to the number of bytes written. - * - * The write() method writes only till n as writting the whole array - * creates problem during the last iteration when all array elements are - * not reinitialized and the last few elemnts contain leftover values from - * the last iteration. - * - */ - private static void transferData() throws IOException { - while((n=input.read(b, 0, buffer)) != -1) { - p_output.write(b, 0, n); - } - } + /* * @@ -120,7 +108,6 @@ private static void transferData() throws IOException { private static void closeCrap() throws IOException { server_socket.close(); socket.close(); - p_output.close(); input.close(); }