It doesn't seem like you are reading the data from the file correctly. When reading data from a stream in Java, the standard practice is to read data into a buffer. The buffer size may be the size of your packet.
File fileToSend =
InputStream in = new FileInputStream(fileToSend);
OutputStream out =
byte buffer[] = new byte[PACKET_SIZE];
int read;
while ((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
in.close();
out.close();
Note that the size of the buffer array remains constant. But - if the buffer cannot be filled (for example, when it reaches the end of the file), the remaining elements of the array will contain data from the last package, so you should ignore these elements (this is what the line does out.write()in my code example)
source
share