gpt4 book ai didi

java - java中没有接收到的字节数

转载 作者:可可西里 更新时间:2023-11-01 17:03:43 25 4
gpt4 key购买 nike

我想确定从以下工作 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");

最后一行的输出如下:

   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

我的代码有什么错误。请帮助我

最佳答案

   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");

关于java - java中没有接收到的字节数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8040030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com