sending a file using DataOutputStream in java

放肆的年华 提交于 2019-12-04 18:03:53

It's quite simple, but the code is a bit long to write it all and sounds like homework.

I can give you some indications.

Just open the file, use the long length() method of the class File to get the size, and the writeLong(long) method of DataOutputStream to send the length to the server. Then just read the file a block at a time and use the write(byte[]) method of DataOutputStream to send each block.

To read a file a block at a time, you will just create a FileInputStream and use its int read(byte[]) method. Be careful not to assume that this metod will fill up the whole buffer, because it is not guaranteed to do. Read the docs!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!