android开发,socket发送文件,read阻塞,得不到文件尾-1

这是我的接收文件代码:开始可以读取到-1,但是现在又读取不到了,所以才加上红色字解决的(注释的代码)文章地址https://www.yii666.com/article/756061.html网址:yii666.com<

                    File file = new File(mfilePath,"chetou."+entity.mediaType);
if(!file.exists())
file.createNewFile();
FileOutputStream fos =new FileOutputStream(file); //将接收的文件保存到对应的路径
byte[] sendBytes =new byte[1024];
int transLen =0;
Log.v(TAG, "----开始接收文件<" + entity.params +">,文件大小为<" + fileLength +">----");
while(true){
int read =0;
read = dis.read(sendBytes);
Log.v(TAG, "read="+read);
if(read == -1)
break;
transLen += read;
Log.v(TAG, "接收文件进度" +100 * transLen/fileLength +"%...");
fos.write(sendBytes,0, read);
fos.flush();
/*if(transLen==fileLength)
break
;*/
}
Log.v(TAG, "----接收文件<" + entity.params +">成功-------1");
entity.filePath = mfilePath+"/chetou."+entity.mediaType; //将下载下来的文件名字赋值给entity.filePath
Log.v(TAG, "----接收文件<" + entity.params +">成功-------2");

发送文件的代码:文章来源地址https://www.yii666.com/article/756061.html网址:yii666.com文章来源地址:https://www.yii666.com/article/756061.html

//传输文件
FileInputStream fis =new FileInputStream(file);
byte[] sendBytes =new byte[1024];
int length =0;
while((length = fis.read(sendBytes,0, sendBytes.length)) >0){
dos.write(sendBytes,0, length);
dos.flush();
}
fis.close();
Log.v(TAG, "发送完文件数据");

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png