我做的一个Applet,当JSP页面打开时,此Applet连接到服务器,从服务器下载一个文件。
现在能大概估算来下载的进度,我想在Applet上显示百分比进度。
现在问题在于,把连接服务器,下载文件的写在Applet的init()方法里,
但是样在下载执行完成之前,所得到的百分比都无法显示出来,
只有在init()里执行完成后,才在Applet上显示一个完成的百分比。
请问有没有办法显示出百分比?

解决方案 »

  1.   

    我的代码如下:import java.applet.*;
    import java.awt.*;
    import java.net.*;
    import java.io.*;public class TestApplet
        extends Applet {
      String msg = "";  public TestApplet() {
      }  public String getParameter(String key, String def) {
        return getParameter(key) != null ? getParameter(key) : def;
      }  public void init() {
        try {
          String remoteURL= "http://localhost/doc/100000001463.dat";
          URL url = new URL(remoteURL);
          URLConnection urlConnection = url.openConnection();
          urlConnection.connect();      FileOutputStream fos = new FileOutputStream("C:/temp/100000001463.dat");
          HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
          InputStream ins = urlConnection.getInputStream();
          int filesize = urlConnection.getContentLength();
          int guage= 0;
          int i = 0;
          while (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK &&
                 i > -1) {
            i = ins.read();
            fos.write(i);
            guage++;
            msg = guage/filesize;
          }
          ins.close();
          fos.close();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }  public void paint(Graphics g) {
        setBackground(Color.white);
        g.drawString(msg, 20, 20);
      }}
    怎么能让msg动态的显示出来,我把这个程序放在页面中,
    只有在Applet全部执行完成后,也就是文件下载完成后,才能显示,所以就只能显示100%,请高手帮忙啊!