java send file using sockets

前端 未结 3 634
长情又很酷
长情又很酷 2020-12-28 11:08

I am trying to send a file from one computer to another using Java. I have written the code below, it works fine if both sender and receiver are started in the same computer

3条回答
  •  隐瞒了意图╮
    2020-12-28 11:43

    Remember that in.read(buffer) not necessarily fills up the whole buffer with new data. Therefore you should make sure you don't write the whole buffer. Change

    while((count=in.read(buffer)) >0){
        fos.write(buffer);
    }
    

    to

    while((count=in.read(buffer)) >0){
        fos.write(buffer, 0, count);
    }
    

提交回复
热议问题