Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы ShowWebFile.java

Оглавление

Назад

// ==========================================
// ShowWebFile.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class ShowWebFile extends Applet
  implements ActionListener, Runnable
{
  TextField urlAddress;
  TextArea txt;
  Button btnGet;

  // ============================================
  // init
  // ============================================
  public void init()
  {
    urlAddress = new TextField("http://", 50);
    txt = new TextArea("", 20, 70);
    btnGet = new Button("Get and show");
    
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = 
      new GridBagConstraints();
    
    setLayout(gbl);
    
    c.fill = GridBagConstraints.HORIZONTAL;  
    c.weightx = 1.0;
    gbl.setConstraints(urlAddress, c);
    add(urlAddress);
    
    c.fill = GridBagConstraints.NONE;  
    c.gridwidth  = GridBagConstraints.REMAINDER; 
    c.weightx = 0.0;
    gbl.setConstraints(btnGet, c);
    add(btnGet);
    
    c.fill = GridBagConstraints.BOTH;  
    c.weighty = 1.0;
    gbl.setConstraints(txt, c);
    add(txt);
    
    btnGet.addActionListener(this);
  }

  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource().equals(btnGet))
    {
      Thread getThread = new Thread(this);
      getThread.start();
    }
  }
  
  // ============================================
  // run
  // ============================================
  public void run()
  {
    String str;
    byte buf[] = new byte[100];
    URL u;
    
    try
    {
      u = new URL(urlAddress.getText());
      InputStream is = u.openStream();
      
      while(true)
      {
        int nReaded = is.read(buf);
	
        if(nReaded == -1)
          break;
	  
        str = new String(buf);
        txt.append(str.substring(0, nReaded));
      }
    }
    catch(Exception ioe)
    {
      showStatus(ioe.toString());
      stop();
    }
  }
}

[Назад]