下面的程序可以实现文件下载,但是不能(或者是我不知道)显示下载的进度,希望高手指点:有没办法在下载文件时显示进度?谢谢了。
package javaapplication2;
import java.net.*;
import java.io.*;
import java.lang.*;public class Main {
    public static final int BUFFER_SIZE=8096;
    public Main() {
    }    public static void main(String[] args) {
        try{
            FileOutputStream fos = null;
            BufferedInputStream bis = null;
            HttpURLConnection httpUrl = null;
            URL url = null;
            byte[] buf = new byte[BUFFER_SIZE];
            int size = 0;
            //建立链接
            url = new URL("http://desktop.google.com/download/googletalk/googletalk-setup.exe");
            httpUrl = (HttpURLConnection) url.openConnection();
            //连接指定的资源
            httpUrl.connect();
            //获取网络输入流
            bis = new BufferedInputStream(httpUrl.getInputStream());
            //建立文件
            fos = new FileOutputStream("g:\\googletalk-setup.exe");
            //保存文件
            int all=0;
            while ( (size = bis.read(buf)) != -1) 
            {
                all+=size;
                fos.write(buf, 0, size);
                System.out.println(String.valueOf((float)all/(float)1341312*100)+"%");
            }
            fos.close();
            bis.close();
            httpUrl.disconnect();
            }
            catch(Exception e)
            {
                System.out.println("Error: "+e.getMessage());
            }
        }
}

解决方案 »

  1.   

    不好意思上面的代码有点问题,要把System.out.println(String.valueOf((float)all/(float)1341312*100)+"%");这句和一些相关的去掉(这是我乱写的~~~~~~~~~):
    package javaapplication2;
    import java.net.*;
    import java.io.*;
    import java.lang.*;public class Main {
        public static final int BUFFER_SIZE=8096;
        public Main() {
        }    public static void main(String[] args) {
            try{
                FileOutputStream fos = null;
                BufferedInputStream bis = null;
                HttpURLConnection httpUrl = null;
                URL url = null;
                byte[] buf = new byte[BUFFER_SIZE];
                int size = 0;
                //建立链接
                url = new URL("http://desktop.google.com/download/googletalk/googletalk-setup.exe");
                httpUrl = (HttpURLConnection) url.openConnection();
                //连接指定的资源
                httpUrl.connect();
                //获取网络输入流
                bis = new BufferedInputStream(httpUrl.getInputStream());
                //建立文件
                fos = new FileOutputStream("g:\\googletalk-setup.exe");
                //保存文件
                while ( (size = bis.read(buf)) != -1) 
                {
                    fos.write(buf, 0, size);
                }
                fos.close();
                bis.close();
                httpUrl.disconnect();
                }
                catch(Exception e)
                {
                    System.out.println("Error: "+e.getMessage());
                }
            }
    }