写一个简单的JAVA下载程序

xiaoxiao2022-06-12  35

问题 1 url = new URL("http://soundofwater.myblog.it/media/01/01/9d7785a88668e552a3e95a2aeeecbd79.mp3"); connection = (HttpURLConnection)url.openConnection(); is = connection.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); long length = url.getFile().length(); System.out.println("file : " + url.openConnection().getContentLength()); System.out.println("long : "+length); FileOutputStream fos = new FileOutputStream("d:\\hehe.mp3"); byte[] bs = new byte[512]; int b ; int i = 0; int n = 0; while((n = is.read(bs))!= -1){ / fos.write(bs,0,n); // i++; } /* while((b = bis.read())!=-1){ fos.write(b); i++; // bis.read(b); // fos.write(b); } */ fos.flush(); System.out.println(" i : "+i); 为什么 一个 4024469 的文件 需要读取 4024469/512= 7861 次 但是却读取了 8012次 把fos.write(bs); 文件大小也相应的增加 改成 fos.write(bs,0,n);文件大小正常了 。 读取次数还是多了。 未解决 找到原因了 是因为网络的传输速度不同 得到的数据每次都不能装满 数组 所以就多读了很多次。 相关资源:如何用java写文件txt
转载请注明原文地址: https://www.6miu.com/read-4933165.html

最新回复(0)