zoukankan      html  css  js  c++  java
  • android开发下载的文件大小始终大于服务器的文件的大小问题解决

    最近学习android开发,写了一个在web服务器下载文件的程序,程序能够下载成功,但是下载下来竟然发现下载的文件和服务器上的文件大小不一致,参考了网上的很多程序,没有发现问题,搞了一夜,终于发现bug,问题出现在往sdcard写入的一段程序:

    byte[] buffer = new byte[4*1024];
    	while(( is.read(buffer)) != -1){//问题出现在这儿
    	//is.read(buffer)不一定正好读入4*1024个字节,测试后发现很少能一次读满buffer,大部分时候是1440字节,不知道什么原因,求教高手!
    	os.write(buffer);
    }

    修改这段程序后(如下所示),运行终于正常,不知道为什么有些网友用上边的程序能运行成功。

    byte[] buffer = new byte[4*1024];
    int length = 0;
    while((length = is.read(buffer)) != -1){
    	os.write(buffer,0,length);
    	System.out.println(length);
    }

  • 相关阅读:
    原码, 反码, 补码 详解
    位移运算符
    ASP.NET中httpmodules与httphandlers全解析
    MySQL count
    真正的能理解CSS中的line-height,height与line-height
    IfcEvent
    IfcWorkCalendarTypeEnum
    IfcSingleProjectInstance
    转换模型
    IfcTypeProduct
  • 原文地址:https://www.cnblogs.com/sunzhenxing19860608/p/1986585.html
Copyright © 2011-2022 走看看