package serveraccept;
import java.net.*;
import java.io.*;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */public class ServerAccept implements Runnable {
        final static int  LISTEN_PORT=8091;
public void run ()
        {
        ServerSocket server=null;
        try {
                server =new ServerSocket(LISTEN_PORT);
                System.out.println(" now wait for connecting .....");
        }
        catch (IOException e) {
         System.out.println("can not connect to the port :"+LISTEN_PORT+":"+e.getMessage());
         System.exit(0);
        }
        while (true) {
                try {
    Socket bb=server.accept();////这里是监听
    new  Thread (new ServerEchoRequest(bb)).start();
//                        new  Thread (new ServerEchoRequest(server.accept())).start(); 
                        System.out.println("now start up a connection ");
                }
                catch (Exception ex) {
                        System.out.println("Canot recieve data " +ex.getMessage() );
                }
        }        }
        public ServerAccept() {
        }
        public static void  main(String[] args) {
           //     ServerAccept serverAccept1 = new ServerAccept();
        //   public static void  main(String[] args) {
      new  Thread(new ServerAccept()).start();
}        }

解决方案 »

  1.   

    其中要用到的!
    package serveraccept;
    import java.net.*;
    import java.io.*;
    import java.util.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class ServerEchoRequest implements Runnable   {
            Socket m_Socket=null;
            final static int  MAX_BUFF=4000;
            private final static int   TIMEOUT=3000;
            public ServerEchoRequest (Socket socket) throws SocketException  {
                    m_Socket=socket;
            }        public void run ()
            {
                try {
                        getClient (m_Socket);
                }
                catch (IOException ex) {
                        System.out.println(ex);
                        System.exit(0);            }
                catch (ClassNotFoundException ex){
                        System.out.println(ex);
                        System.exit(0);
                }
            }
            protected void getClient(Socket socket)throws IOException,ClassNotFoundException{
            DataInputStream in=new DataInputStream(m_Socket.getInputStream());
            String s;
            File f=new File("request.txt");
            PrintWriter fileout=new PrintWriter(new FileWriter(f));
            while ((s=in.readLine())!=null) {
                    //System.err.println("");
                    System.out.println(s);
                    fileout.println(s);
                    fileout.flush();
            }
            }/*        public static void main(String[] args) {
                  ServerEchoRequest serverEchoRequest1 = new ServerEchoRequest();
            }
    */
    }
      

  2.   

    干脆把测试环境也给你把:
    <html>
    <head>
    <title>
    jsp1
    </title>
    </head>
    <body bgcolor="#ffffff">
    <h1>
    Display upload file request
    </h1>
    <form method ="post" action ="http://localhost:8091" enctype="multipart/form-data">
    <p> &nbsp;ID:<input type ="text" name ="FileID" size="20" ></p>
    <p>file:<input type ="file" name="filedata" size ="20" ></p>
    <p><input type="submit" value ="Upload" name="uploadfile"></p>
    </form>
    </body>
    </html>