Good attempt, but not entirely correct.
Your only mistake (as far as I see) are these lines:
out.write('\n');
out.write((int) imageFile.length());
This is wrong because: you don't know the size of that integer. If the image is 10,000 bytes, it'll be represented by a 6 character string, but...