1

I need to change this code so that it will send a directory of image files at the moment it currently only sends one file, my main goal is to have it ask for a directory and then send all of the files in that directory (image files) to the server i then need it to display how much data was sent the code i currently have is:

Client:

package sockets;
import java.net.*;
import java.io.*;

public class Client {

    public static void main (String [] args ) throws IOException {
        int filesize=1022386;
        int bytesRead;
        int currentTot = 0;
        Socket socket = new Socket("127.0.0.1",6789);
        byte [] bytearray  = new byte [filesize];
        InputStream is = socket.getInputStream();
        FileOutputStream fos = new FileOutputStream("copy.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(bytearray,0,bytearray.length);
        currentTot = bytesRead;
        System.out.println("The Size of the data transferred is " + bytesRead + " Bytes");

        do {
           bytesRead =
              is.read(bytearray, currentTot, (bytearray.length-currentTot));
           if(bytesRead >= 0) currentTot += bytesRead;
        } while(bytesRead > -1);

        bos.write(bytearray, 0 , currentTot);
        bos.flush();
        bos.close();
        socket.close();
      }
}

Server:

package sockets;
import java.net.*;
import java.io.*;
public class Server {


 public static void main (String [] args ) throws IOException {`

            ServerSocket serverSocket = new ServerSocket(6789);
              Socket socket = serverSocket.accept();
              System.out.println("Accepted connection : " + socket);
              File transferFile = new File ("Orders.txt");
              byte [] bytearray  = new byte [(int)transferFile.length()];
              FileInputStream fin = new FileInputStream(transferFile);
              BufferedInputStream bin = new BufferedInputStream(fin);
              bin.read(bytearray,0,bytearray.length);
              OutputStream os = socket.getOutputStream();
              System.out.println("Sending Files...");
              os.write(bytearray,0,bytearray.length);
              os.flush();
              socket.close();
              System.out.println("File transfer complete");
            }
}

Thank you

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
user2698097
  • 11
  • 1
  • 2

2 Answers2

1

Iterate over all files in the selected directory and get all the known image extensions you want to send.

Here's an example for iterating over files.

Then, stream the bytes in each of those files from your client to your server.

I suggest using FTP for sending your files to your server as its an established protocol for precisely this type of problem.

Community
  • 1
  • 1
William Morrison
  • 10,953
  • 2
  • 31
  • 48
0

It isn't possible to send the directory completely, as is. You have 2 options:

  1. Create a zip file, and send that.
  2. Open the directory and iterate over the entire directory and send each file individually.
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • If i took the second option could you show an example piece of code on how i would have it iterate the directory thanks – user2698097 Aug 19 '13 at 23:55