byte[] buffer=new byte[4*1024]; int len; while((len=input.read(buffer))!=-1){ //the 0 is the starting of buffer,not the starting of ouput; output.write(buffer,0,len); } output.flush();
the 0 and len means that the section of the buffer, not the section of the output stream. The outputStream pointer will keep go on as long as something write to it .
Every one while loop write a 4*1024 buffer to buffer . When it touch the final part of the file . it will return the real length of the final part of the file .
If not point out the len in the output stream write(). It will write 1024 bytes instead writing the real length.