Can't send large files over socket in Java

前端 未结 5 1684
执念已碎
执念已碎 2021-01-14 06:51

I got working server and client applications, they work perfect while sending small files, but when I try to send for example movie file that is 700mb over socket it gives m

5条回答
  •  半阙折子戏
    2021-01-14 07:11

    The problem is that in your client you create a byte array for the entire file.

    byte[] fileLength = new byte[(int) file.length()];  //potentially huge buffer allocated here
    

    You should do the same thing you do on the server side and read the file chunk by chunk into a fixed size buffer.

提交回复
热议问题