简体   繁体   English

在java中没有收到的字节数

[英]No number of bytes received in java

I would like to determine the number of bytes downloaded from the following working URL connection: I have following code to implement: 我想确定从以下工作URL连接下载的字节数:我有以下代码来实现:

   .......
   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   connection.connect();
   InputStream is = connection.getInputStream();   // throws an IOException
   DataInputStream dis = new DataInputStream(new BufferedInputStream(is));          

   FileOutputStream fos = new FileOutputStream("C:\\Picture.jpeg");
   int read =0;
   byte[] bytes = new byte[1024];
   while((read = dis.read(bytes)) != -1)
   {
        fos.write(bytes, 0, read);
   }
   System.out.println(read + " byte(s) copied");

The output from the last line is as follows: 最后一行的输出如下:

   Opening connection to http://www.xyz.com//Picture.jpeg...
   Copying image resource (type: application/jpeg, modified on: 02/02/2010 4:19:21 AM)...
  -1 byte(s) copied

What is the error of my code. 我的代码有什么错误? please help me 请帮我

   int read =0;
   int reddit = 0;
   byte[] bytes = new byte[1024];
   while((read = dis.read(bytes)) != -1)
   {
        fos.write(bytes, 0, read);
        reddit += read;
   }
   //your read variable must have the value -1 at this point
   System.out.println(reddit + " byte(s) copied");
int totalBytes = 0;
...
while((read = dis.read(bytes)) != -1)
{
    totalBytes += read;
    fos.write(bytes, 0, read);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM