• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting filesize of an HTML file in an Applet

 
harsh tib
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
I have written a small applet (the code is produced below).
OBJECTIVE OF THE FOLLOWING APPLET : To get the filesize and lastmodified time of an HTML file from an URL after every 3 second.
ERROR : The results of this applet is behaving very inconsistently. Sometimes I am getting the values and mostly the thread stops working after 10-12 loops.
I am really surprised why this thread is stopping automatically. Pls help on what is the way out ?
//////// CODE OF APPLET /////////
/* START */
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;
public class URLChatApplet extends Applet implements Runnable
{
String myAppletLocation;
String myfilename;
TextArea Tx1;
Thread t1;
public void init(){
myAppletLocation = "http://www.myserver.com/mydirectory/";
myfilename = "myfile.html";
Tx1 = new TextArea("", 10, 30);
add(Tx1);
t1 = new Thread(this,"updates");
t1.start();
}
private void updMessages(){
String msg="";
try{
URL fileOfInterest = new URL(myAppletLocation + myfilename);
URLConnection connectionToFileOfInterest = fileOfInterest.openConnection();
int contentLength = connectionToFileOfInterest.getContentLength();
long lastModifiedTime = connectionToFileOfInterest.getLastModified();
String mystr = new String ("Content length : "+contentLength+"\nLastModified Time : "+lastModifiedTime+"\n");
Tx1.append(mystr);
connectionToFileOfInterest = null;
fileOfInterest = null;
}
catch(Exception e)
{
msg = "General Exception : "+e.getClass().getName()+" : "+e.getMessage();
}
return;
}
public void run(){
try{
while(true){
updMessages();
t1.sleep(3000);
}
}catch(Exception e)
{
System.out.println("General Exception : "+e.getClass().getName()+" : "+e.getMessage());
}
}
}
/* END */
Regards
Harsh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic