Java 为什么我的BufferedInputStream没有下载某些文件?

Java 为什么我的BufferedInputStream没有下载某些文件?,java,bufferedinputstream,Java,Bufferedinputstream,这是我的密码 while ((count = in.read(data, 0, 16384)) != -1) // while there is still data from link to be read { download.removeMouseListener(m); /*speed.setText(String.valueOf(count));*/ if (time >= time2 ){ // every second , set kb/s

这是我的密码

while ((count = in.read(data, 0, 16384)) != -1) // while there is still data from link to be read
{   
    download.removeMouseListener(m);
    /*speed.setText(String.valueOf(count));*/
    if (time >= time2 ){ // every second , set kb/s
        long initTimeInSeconds = initTime/1000000000;
        long currentTimeInSeconds = time/1000000000;
        speed.setText(String.valueOf((fout.getChannel().size() / 1024) / (currentTimeInSeconds-initTimeInSeconds) + "KB/s")); // set kb/s
        time2 = System.nanoTime() + 1000000000;
    }

    progress.setValue((int) fout.getChannel().size());//update progress bar
    fout.write(data, 0, count); // write the data to the file
    time = System.nanoTime();
} 
基本上,下载代码如下所示:

while ((count = in.read(data, 0, 16384)) != -1) // while there is still data from link to be read
{   
    fout.write(data, 0, count); // write the data to the file
    time = System.nanoTime();
} 
在哪里

byte data[] = new byte[16384];
BufferedInputStream in = null;
in = new BufferedInputStream(url.openStream());
int count = 0;
不知怎的,这段代码没有下载某些文件,例如这个文件。

while循环在开始时就消失了,它读取了2次,然后完全停止


谁能解释一下原因吗?

这段代码对我来说很好用。下面的代码下载文件并写入磁盘

            URL url = new URL("http://nmap.org/dist/nmap-6.40-setup.exe");
    url.openConnection();
    byte data[] = new byte[16384];
    BufferedInputStream in = null;
    in = new BufferedInputStream(url.openStream());
    int count = 0;
    OutputStream fout =  new FileOutputStream("C:/test.exe");
    while ((count = in.read(data, 0, 16384)) != -1) // while there is still data from link to be read
    {   
        fout.write(data, 0, count); // write the data to the file
        long time = System.nanoTime();
        System.out.println(time);
    } 
    fout.flush();
    fout.close();
            URL url = new URL(logoPath);
            in = new BufferedInputStream(url.openStream());
            while (true) {

                if (in.available() == Integer.parseInt(app.getCoversize()))
                    break;
            }
我看到的唯一可能的失败是,如果输出流没有正确关闭。可能无法将文件刷新到磁盘中

            URL url = new URL("http://nmap.org/dist/nmap-6.40-setup.exe");
    url.openConnection();
    byte data[] = new byte[16384];
    BufferedInputStream in = null;
    in = new BufferedInputStream(url.openStream());
    int count = 0;
    OutputStream fout =  new FileOutputStream("C:/test.exe");
    while ((count = in.read(data, 0, 16384)) != -1) // while there is still data from link to be read
    {   
        fout.write(data, 0, count); // write the data to the file
        long time = System.nanoTime();
        System.out.println(time);
    } 
    fout.flush();
    fout.close();
            URL url = new URL(logoPath);
            in = new BufferedInputStream(url.openStream());
            while (true) {

                if (in.available() == Integer.parseInt(app.getCoversize()))
                    break;
            }

文件大小=app.getCoversize()

谢谢回复,我发现问题出在我的学校网络上,很明显我的应用程序不想使用它。可能是端口号,是否可以更改我下载文件时使用的端口?您不能更改端口。服务器必须要求客户端更改端口,注意客户端确定时间间隔=getInterval()文件大小=app.getLogosize()