How to send a list of files over a socket in Java

I used the code here to send a single file through a socket. However, I need to be able to send multiple files (basically all the files in the directory) through the socket and the client will find out how the separation between the files is. Honestly, I will completely lose what to do. Any advice would be helpful.

NOTE 1: I need a way to send files to one continuous stream, which the client can split into separate files. He cannot rely on separate requests from the client.

NOTE 2: To answer the question, I am sure that I will receive comments, no, this is NOT homework.

EDIT has been suggested that I can send the file size in front of the file itself. How can I do this, since sending a file over a socket is always performed either in a predefined byte array, or in a single byte separately, and not in the long one returned by File.length()

+8
java sockets
source share
5 answers

Here is the full implementation:

Sender's side:

 String directory = ...; String hostDomain = ...; int port = ...; File[] files = new File(directory).listFiles(); Socket socket = new Socket(InetAddress.getByName(hostDomain), port); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(files.length); for(File file : files) { long length = file.length(); dos.writeLong(length); String name = file.getName(); dos.writeUTF(name); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); int theByte = 0; while((theByte = bis.read()) != -1) bos.write(theByte); bis.close(); } dos.close(); 

Receiver Side:

 String dirPath = ...; ServerSocket serverSocket = ...; Socket socket = serverSocket.accept(); BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); DataInputStream dis = new DataInputStream(bis); int filesCount = dis.readInt(); File[] files = new File[filesCount]; for(int i = 0; i < filesCount; i++) { long fileLength = dis.readLong(); String fileName = dis.readUTF(); files[i] = new File(dirPath + "/" + fileName); FileOutputStream fos = new FileOutputStream(files[i]); BufferedOutputStream bos = new BufferedOutputStream(fos); for(int j = 0; j < fileLength; j++) bos.write(bis.read()); bos.close(); } dis.close(); 

I have not tested it, but I hope it works!

+17
source share

You can send the file size first before each file, so the client will know when the current file has finished and expect the next one (size). This will allow you to use one continuous stream for all files.

+2
source share

A very simple way to do this is to send the file length before sending each file so that you can determine the separation between the files.

Of course, if the receiving process is Java, you can just send the objects.

+1
source share

You can archive files on the client side and send this archived stream to the server.

for example: http://www.exampledepot.com/egs/java.util.zip/CreateZip.html

from...

 OutputStream output = connection.getOutputStream(); ZipOutputStream out = new ZipOutputStream(output); 
0
source share

Perhaps the fastest way is to automatically download and decompress files in your directory into a single file, see the java.util.zip package

0
source share

All Articles