求救一个Applet问题,请高手指点~

lxf0088 2006-06-22 11:11:28
我做的一个Applet,当JSP页面打开时,此Applet连接到服务器,从服务器下载一个文件。
现在能大概估算来下载的进度,我想在Applet上显示百分比进度。
现在问题在于,把连接服务器,下载文件的写在Applet的init()方法里,
但是样在下载执行完成之前,所得到的百分比都无法显示出来,
只有在init()里执行完成后,才在Applet上显示一个完成的百分比。
请问有没有办法显示出百分比?
...全文
95 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxf0088 2006-06-23
  • 打赏
  • 举报
回复
在线等待,,,高手进来啊。。
lxf0088 2006-06-23
  • 打赏
  • 举报
回复
我的代码如下:

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%,请高手帮忙啊!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧