Java - File Transfer Example 2

This code snippet demonstrates a simple console based file transfer. The server listens for incoming connections and can connect to multiple clients. On connecting to the server, the client sends the filesize and filename of the sending file to the server in two separate transmissions. On the third transmission, the client sends the file. The server is made aware of the filesize and the filename and proceeds to receive the file from the client. The filename is supplied as an argument when executing the client from the command-line.

Usage

  1. java Client  "PATH_TO_FILE"  

Client

  1. import java.net.*;  
  2. import java.io.*;  
  3.   
  4. class Client{  
  5.   
  6.     public static void main(String args[]){  
  7.   
  8.         try {  
  9.           
  10.             // Command argument  
  11.             String fileName = args[0];  
  12.               
  13.             Socket skClient = new Socket( "127.0.0.1", 1234);  
  14.               
  15.             System.out.println( "Connected to server!!!");  
  16.               
  17.             File file = new File(fileName);  
  18.             FileInputStream fReader = new FileInputStream(file);  
  19.               
  20.             OutputStreamWriter sWriter = new OutputStreamWriter(skClient.getOutputStream());  
  21.               
  22.             // Send file size to server.  
  23.               
  24.             String fileSize = Long.toString(file.length());  
  25.             sWriter.write(fileSize+ "\n");  
  26.             sWriter.flush();  
  27.               
  28.             // Send filename to server.  
  29.               
  30.             sWriter.write(file.getName()+ "\n");  
  31.             sWriter.flush();  
  32.               
  33.             byte[] buffer = new byte[Integer.parseInt(fileSize)];  
  34.   
  35.             int bytesRead = 0;  
  36.   
  37.             OutputStream oStream = skClient.getOutputStream();  
  38.               
  39.             System.out.println( "Sending file to server.");  
  40.               
  41.             // Send the file to the server.  
  42.             while ((bytesRead = fReader.read(buffer))>0)  
  43.             {  
  44.                 oStream.write(buffer,0,bytesRead);  
  45.             }  
  46.               
  47.             oStream.close();  
  48.             fReader.close();  
  49.             skClient.close();  
  50.         }  
  51.         catch (Exception e){  
  52.             System.out.println(e.getMessage());  
  53.         }  
  54.     }  
  55. }  

Server

  1. import java.net.*;  
  2. import java.io.*;  
  3.   
  4. class Server{  
  5.   
  6.     public static void main(String args[]){  
  7.         System.out.println( "Server started");  
  8.           
  9.         try {  
  10.             ServerSocket skServer = new ServerSocket(1234);  
  11.               
  12.             while (true){  
  13.                 Socket skClient = skServer.accept();  
  14.                   
  15.                 InputStreamReader sReader = new InputStreamReader(skClient.getInputStream());  
  16.                 BufferedReader bReader = new BufferedReader(sReader);  
  17.                   
  18.                 // Receive filesize from client  
  19.                 String fileSize = bReader.readLine();  
  20.                   
  21.                 // Receive filename from client  
  22.                 String fileName = bReader.readLine();  
  23.                   
  24.                 System.out.println( "File Size: " + fileSize);  
  25.                 System.out.println( "Filename: " + fileName);  
  26.                   
  27.                 // Create a file in the same location as the running server.  
  28.                   
  29.                 FileOutputStream oStream = new FileOutputStream(new File(fileName));  
  30.   
  31.                 byte[] buffer = new byte[Integer.parseInt(fileSize)];  
  32.   
  33.                 int bytesReceived = 0;  
  34.   
  35.                 InputStream iStream = skClient.getInputStream();  
  36.                   
  37.                 while ((bytesReceived = iStream.read(buffer))>0)  
  38.                 {  
  39.                     /* Write to the file */  
  40.                    oStream.write(buffer,0,bytesReceived);  
  41.                 }  
  42.                   
  43.                 oStream.close();  
  44.                 iStream.close();  
  45.             }  
  46.         }  
  47.         catch (Exception e){  
  48.             System.out.println(e.getMessage());  
  49.         }  
  50.     }  
  51. }  

No comments:

Post a Comment