import java.awt.*;
import java.applet.*;
import java.io.*;
import java.util.*;
import java.net.*;
public class Happy extends Applet
    {
    private TextArea textArea = new TextArea (25, 70);    public void init ()
{
try
    {
    URL url;
    URLConnection urlConn;
    DataOutputStream printout;
    DataInputStream input;     // URL of CGI-Bin script.
    url = new URL (getCodeBase().toString() + "env.cgi");     // URL connection channel.
    urlConn = url.openConnection();     // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);     // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);     // No caching, we want the real thing.
    urlConn.setUseCaches (false);     // Specify the content type.
    urlConn.setRequestProperty
("Content-Type", "application/x-www-form-urlencoded");     // Send POST output.
    printout = new DataOutputStream (urlConn.getOutputStream ());     String content =
"name=" + URLEncoder.encode ("Buford Early") +
"&email=" + URLEncoder.encode ("[email protected]");
    
    printout.writeBytes (content);
    printout.flush ();
    printout.close ();     // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());     String str;
    while (null != ((str = input.readLine())))
{
System.out.println (str);
textArea.appendText (str + "\n");
}     input.close ();     // Display response.
    add ("Center", textArea);
    }
catch (MalformedURLException me)
    {
    System.err.println("MalformedURLException: " + me);
    }
catch (IOException ioe)
    {
    System.err.println("IOException: " + ioe.getMessage());
    }
} // End of method init().
    } // End of class Happy.

解决方案 »

  1.   

    直接用socket就可以了,如:
    Socket st = new Socket("http://www.csdn.net/Expert/topic/432/432228.shtm",80);
    BufferedInputStream bis = st.getBufferedInputStream();
    String s=null;
    s = bis.readLine();
    while(s!=null){
    ;写入文件
    s = bis.readLine();
    }
      

  2.   

    有没有只用java.net.url 和java.io类做的,程序要短一些?
      

  3.   

    import java.net.*; // For Socket stuff
    import java.util.*; // For StringTokenizer
    import java.io.*; // For PrintStream//============================================================================
    // Takes a URL as input and retrieves the file. Only handles the http
    // protocol. Does a direct connection rather than using the URLConnection
    // object in order to illustrate the use of sockets.
    //
    // 8/96 Marty Hall, [email protected]
    // http://www.apl.jhu.edu/~hall/java/
    //============================================================================public class GetURL {public static int port = 80;public static void main(String[] args) {
    StringTokenizer tok = new StringTokenizer(args[0]);
    String protocol = tok.nextToken(":");
    String host = tok.nextToken(":/");
    String uri;
    if (tok.hasMoreTokens())
    uri = "/" + tok.nextToken(" ") + "/";
    else
    uri = "/";
    System.out.println("Protocol=" + protocol + ", host=" + host +
    ", uri=" + uri);
    try {
    Socket clientSocket = new Socket(host, port);
    PrintStream outStream = new PrintStream(clientSocket.getOutputStream());
    DataInputStream inStream =
    new DataInputStream(clientSocket.getInputStream());
    outStream.println("GET " + uri + " HTTP/1.0\n");
    String line;
    while ((line = inStream.readLine()) != null)
    System.out.println("> " + line);
    } catch(IOException ioe) {
    System.out.println("IOException:" + ioe);
    } catch(Exception e) {
    System.out.println(e.fillInStackTrace());
    System.out.println(e.getMessage());
    e.printStackTrace();
    System.out.println("Error! " + e);
    }
    }
    }//============================================================================
      

  4.   

    程序中要定义类和对象,并且要使用类和对象!!! 因为程序的目的就是为了展现面向对象的特点和优点!!!
    假定: 对一家公司的员工进行管理:
    包括人员名字,出生日期,加入公司的年份,那个部门(用ring0---ring9表示10个部门) ,工资标准等(多多不限)
    要求有对(假如有员工改名, 可以实现改名, 尽管使用中不常见, 但主要是为了实现面向对象思想)(更改工资标准)(开除员工和增加员工)(通过工龄即加入公司的时间来对员工进行排序, 同一年的可随便排)(通过部门的对员工排序, 个部门的集中在一起显示,譬如ring0的排了下了跟着就是全是ring1部门的)
    请兄弟们快点回帖, 把源代码和程序发给我, 搞定后就给200分!!!
    这是我期末考试的题目, 兄弟们要快快快!!
     我的email :  [email protected]
      

  5.   

    我需要的程序找到了,如下:
    // useURL.java
    import java.net.*; // 引 入 类 库
    import java.io.*;
    class useURL{
      public static void main(String args[]){
        try{
          URL resource = new URL("http://202.102.240.73/index.html"); 
          String oneline; // 下 一 条 语 句 建 立 数 据 流
          DataInputStream stream = new DataInputStream(resource.openStream());
          while((oneline = stream.readLine( ))!= null) // 将数 据 一行 行 读 入 , 存 入 字 符 串 oneline
          {System.out.println(oneline);} // 将 数 据 输 出 到屏 幕
           stream.close( ); // 关 闭 数 据 流 
       }
       catch(MalformedURLException e){ // 异 常 处 理
       System.out.println("MalformedURLException:"+e);}
       catch(IOException IOe){
       System.out.println("IOException:"+IOe);}
      }
    } 呵呵,是不是太弱我?还是谢谢大家说的,对我启发很大,给分!