如何利用 Java 实现在网络中传送文件呢? (请指点方向, 谢谢!)

解决方案 »

  1.   

    import java.net.*;
    import java.io.*;
    public class Server
    {
      private ServerSocket server;
      private Socket you;
      private receive rec;
      public Server()
      {
        try
        {
         server = new ServerSocket(2007);
         System.out.println("服务器运行中,监听端口:2007......");
         while(true)
          {
           you = server.accept();
           if(you != null)
    {
    System.out.println("有客户连接启动接收线程...");
             new Thread(new receive(you)).start();
    System.out.println("接收文件内容如下: --->服务器继续监听中...");
    }
    }
    }catch(Exception e){System.out.println("服务端运行出错!");}
    }public static void main(String args[])
    {
    new Server();
    }
    }class receive implements Runnable
    {
    private File files;
    private DataInputStream din = null;
    private DataOutputStream dout = null;
    private Socket mySock = null;
    private int str = 1;
    public receive(Socket you)
    {
    this.mySock = you;
    //files = new File("d:\\data.txt");
    }public void run()
    {
    try
    {
    din = new DataInputStream(mySock.getInputStream());
    dout = new DataOutputStream(mySock.getOutputStream());while(true)
    {
    if((str=din.readInt()) == 0)
    break;
    System.out.println("" + din.readUTF());
    }
    clears();
    }catch(Exception e){System.out.println("接收出错!");}
    finally
    {
    clears();
    }}void clears()
    {
    try
    {
    dout.close();
    din.close();
    mySock.close();
    }catch(Exception e){System.out.println("关闭出错");}
      }}
      

  2.   

    import java.net.*;
    import java.io.*;
    public class Client
    {
    private Socket you;
    SendData sends;public Client()
    {
    try
    {   
      you = new Socket("localhost",2007);
    System.out.println("连接服务器监听端口:2007......");
              if(you != null)
     {
        System.out.println("连接成功,启动发送线程...");
        new Thread(new SendData(you)).start();
        System.out.println("发送完毕!关闭线程...");
       }
    }catch(Exception e){System.out.println("服务端运行出错!");}
    }public static void main(String args[])
    {
    new Client();
    }
    }class SendData implements Runnable
    {
    private File files;
    private BufferedReader bin = null;
    private DataInputStream din = null;
    private DataOutputStream dout = null;
    private Socket mySock = null;
    private String data = "";
    public SendData(Socket you)
    {
    this.mySock = you;
    files = new File("d:\\data.txt");
    }public void run()
    {
    try
    {
    bin = new BufferedReader(new InputStreamReader(new FileInputStream(files)));
    din = new DataInputStream(mySock.getInputStream());
    dout = new DataOutputStream(mySock.getOutputStream());
    while((data = bin.readLine()) != null)
    {
    dout.writeInt(1);
    dout.writeUTF(data);
    }
    dout.writeInt(0);
    clears();
    }catch(Exception e){}
    finally
    {
    clears();
    }}void clears()
    {
           try
           {
    bin.close();
    dout.close();
    din.close();
    mySock.close();
    }catch(Exception e){System.out.println("关闭出错");}
      }}
    也是给上面一个朋友写的支持多线程,不过是针对一个具休路径的文件,
    服务器方面只是显示出来
    大概原理就是这样,你改下,或者加个用户界面都可以,参考下
      

  3.   

    去这里看看吧:
    http://www.javadingle.com
      

  4.   

    最简易的socket通信,建立一个ServerSocket和一个Socket