Server

public static void activ(ServerSocket serverSocket) throws IOException { Socket socket = serverSocket.accept(); InputStream sin =socket.getInputStream(); OutputStream sout = socket.getOutputStream(); FileInputStream fin = new FileInputStream(new File("Log.txt")); InputStream sfin = new BufferedInputStream(fin); int res = IOUtils.copy(sfin, sout); fin.close(); sfin.close(); sout.flush(); fin = new FileInputStream(new File("Client.png")); sfin = new BufferedInputStream(fin); res = IOUtils.copy(sfin, sout); sout.flush(); sout.close(); socket.close(); 

Client

 public class Main { public static void main(String[] args) throws IOException { Socket socket = new Socket("127.0.0.1", 15123); InputStream in = socket.getInputStream(); FileOutputStream fout = new FileOutputStream(new File("Log.txt")); OutputStream out = new BufferedOutputStream(fout); int res = IOUtils.copy(in,out); out.close(); out.flush(); fout = new FileOutputStream(new File("Client.png")); out = new BufferedOutputStream(fout); res = IOUtils.copy(in,out); out.flush(); out.close(); in.close(); //bos.flush(); //bos.close(); socket.close(); } 

}

I am trying to transfer files using IOUtils.copy (), but I have encountered such a problem that the program adds all the files to one file. I will give an example to make it a little clearer: I am trying to transfer 2 files "Log.txt" and "Client.png". Upon completion of the program, these 2 files become one, i.e. shuffled. Who knows how to solve this problem? The file "Log.txt" itself looks like this:

 Update /images Client.png Small Update /images Close.png Small 

But at the exit he gives me the following:

 Update /images Client.png Small Update /images Close.png Small<содержимое Client.png> 

    1 answer 1

    Before transferring the file itself, transfer its length (and other information about the file — name, creation date, etc.). The receiver reads this meta information and creates files of the correct size, with the correct attributes.

    • Is it possible to somehow indicate in the server part that the transfer of this file is completed? Because earlier I just transferred files without specifying their dimensions and everything worked, but it was half a year ago and I already forgot about it. - Dmitry Lin
    • You can close the channel. and for the next file to create it again. - Mikhail Vaysman
    • those. for each file to create a separate channel? And if I have a file list? - Dmitry Lin
    • @DmitryLin just use my idea. - Mikhail Vaysman