如题.能够给代码看看吗?

解决方案 »

  1.   

    //Client.javaimport java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Client extends JFrame{
    private JTextField enter;
    private JTextArea display;
    ObjectOutputStream output;
    ObjectInputStream input;
    public Client(){
    super("Client");
    Container c =getContentPane();
    enter=new JTextField();
    enter.setEnabled(false);
    enter.addActionListener(new ActionListener(){
    public void axtionPerformed(ActionEvent e){
    sendData(e.getActionCommand());
    }
    });
    c.add(enter,BorderLayout.NORTH);
    display=new JTextArea();
    c.add(new JScrollPane(display),BorderLayout.CENTER);
    setsize(300,150);
    show();
    }
    public void runClient(){
    Socket client;
    try{
    //step1:create a socket connection.
    display.setText("Attempting connection\n");
    client=new Socket(InetAddress.getByName("127.0.0.1"),5000);
    display.append("connected to :"+client.getInetAddress().getHostName());
    //step 2:get the input and output streams.
    output=new ObjectOutputStream(client.getOutputStream());
    output.flush();
    input=new ObjectInputStream(client.getInputStream());
    display.append("\n GOT I/O streams\n");
    //step 3:process connection.
    enter.setEnabled(true);
    do{
    try{
    message=(String) input.readObject();
    display.append("\n"+message);
    display.setCaretPosition(display.getText().length());
    }catch( ClassNotFoundException cnfex ){
    display.append("\nUnknown object type received");
    }
    }while(!message.equals("SERVER>>>TERMINATE"));
    display.append("Closing connection.\n");
    input.close();
    output.close();
    client.close();
    }
    catch(EOFException eof){
    System.out.println("Server terminated connection");
    }
    catch(IOException e){
    e.printStackTrace();
    }
    }
    private void sendData(String s){
    try{
    message=s;
    output.writeObject("CLIENT>>>"+s);
    output.flush();
    display.append("\nCLIENT>>>"+s);
    }catch(IOException cnfex){
    display.append("\nError writing object");
    }
    }
    public static void main(String args[]){
    Client app=new Client();
    app.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    app.runClient();
    }
    }
    //Server.java
    import java.io.*;
    import java.net.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Server extends JFrame {
    private JTextField enter;
    private JTextArea display;
    ObjectOutputStream output;
    ObjectInputStream input;
    public Server()
    {
    super("Server");
    Container c =getContentPane();
    enter=new JTextField();
    enter.setEnabled(false);
    enter.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    sendData(e.getActionCommand());
    }
    });
    c.add(enter,BorderLayout.NORTH);
    display=new JTextArea();
    c.add(new JScrollPane(display),BorderLayout.CENTER);
    setSize(300,150);
    show();
    }
    public void runServer(){
    ServerSocket server;
    Socket connection;
    int counter=1;
    try{
    //第一步:create a ServerSocket.
    server=new ServerSocket(5000,100);
    while(true){
    //第二步:wait for a connection.
    display.setText("等待连接\n");
    connection=server.accept();
    display.append("connection"+counter+" received from: "+connection.getInetAddress().getHostName());
    //step 3:获得input和output 流
    output=new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input=new ObjectInputStream(connection.getInputStream());
    display.append("\n Got I/O stream \n");
    //step 4:连接过程.process connection.
    String message="SERVER>>> Connection successful";
    output.writeObject(message);
    output.flush();
    enter.setEnabled(true);
    do{
    try{
    message=(String) input.readObject();
    display.append("\n"+message);
    display.setCaretPosition(display.getText().length());
    }
    catch(ClassNotFoundException cnfex) {
    display.append("\n Unknown object type received");
    }
    }while(!message.equals("CLIENT>>> TERMINATE"));
    //step 5:close connection.
    display.append("\n USER terminated connection");
    enter.setEnabled(false);
    output.close();
    input.close();
    connection.close();
    ++counter;

    }
    }
    catch(EOFException eof){
    System.out.println("Client terminated connection");

    }
    catch(IOException io){
    io.printStackTrace();
    }
    }
    private void sendData(String s){
    try{
    output.writeObject("SERVER>>>"+s);
    output.flush();
    display.append("\nSERVER>>>"+S);
    }
    catch(IOException cnfex){
    display.append("\nError writing object");
    }
    }
    public static void main(String args[]){
    Server app=new Server();
    app.addWindowListener({
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    app.runServer();
    }
    }
    谁能够帮我添加文件传输功能呢?
      

  2.   

    兄弟的问题和我的毕业设计差不多呢.哈哈.我发给你吧[email protected].
      

  3.   

    这里有个类似QQ的工具,程序在这里
    http://tomore.com/1/38258.html
      

  4.   

    Java 网络文件传输 
     
    读者范围: 本文是一篇简短入门文章.本文假设读者对Java的IO系统和Java的网络系统有所了解. 正文: 关于文件传输的问题,实际也是一种IO读写的基本问题.对于网络而言也是一种IO读写问题.因此所谓网络的文件传输实际是两种IO问题的综合讨论.这里我们首先分析一个图示.然后围绕这个图示来讨论: 
    图1: 分析图1我们基本可以知道从服务器文件系统中通过流把文件中的数据写入到服务器的进程中,然后把进程中的数据通过网络IO系统传递到客户机,这个阶段,网络中的数据以字节流的形式保存.当该字节流被客户进程接受后,客户进程通过客户本地文件流写入客户本地的文件系统中. 根据以上分析,我们基本可以确定我所需要处理的问题了.首先我们需要可以对本地文件系统IO操作的操作接口,然后是一个可以对网络IO系统进行操作的操作接口,已经一个可以把数据包装成字节流的操作接口,他们分别可以提供客户和服务器两个进程进行读写的操作.如下图所示: 图2: 根据以上分析,我们可以把问题归结到对以下编程接口的需求上: 1. 字节包装器和字节解包器, 2. 网络传输器和网络接收器 3. 本地文件读/写器 而这些Java本身的API就已经提供.他们都被包装到java.io和java.net这两个包里,这里我提供一个基于TCP/IP的实现版本,使用基于连接的方式来完成工作.我们首先介绍几个相关的JDK中的类来完成以上任务, 1. DataOutputStream和DataInputStream实现类提供了上面的字节包装和解包器的实现 2. ServerSocket和Socekt提供了基于连接的网络传输和接受接口 3. File,FileInputStream和FileOutputStream提供了基本的本地文件输入输出接口. 服务器端实现代码: import java.io.*; import java.net.*; public class FileServer{ public static void main(String[] args)throws Exception{ //创建文件流用来读取文件中的数据 File file=new File("lishengjie.jpg"); FileInputStream fos=new FileInputStream(file); //创建网络服务器接受客户请求 ServerSocket ss=new ServerSocket(3108); Socket client=ss.accept(); //创建网络输出流并提供数据包装器 OutputStream netOut=client.getOutputStream(); OutputStream doc=new DataOutputStream(new BufferedOutputStream(netOut)); //创建文件读取缓冲区 byte[] buf=new byte[2048]; int num=fos.read(buf); while(num!=(-1)){//是否读完文件 doc.write(buf,0,num);//把文件数据写出网络缓冲区 doc.flush();//刷新缓冲区把数据写往客户端 num=fos.read(buf);//继续从文件中读取数据 } fos.close(); doc.close(); } } 客户方实现代码: import java.io.*; import java.net.*; public class FileClient{ public static void main(String[] args)throws Exception{ //使用本地文件系统接受网络数据并存为新文件 File file=new File("newFile.jpg"); file.createNewFile(); RandomAccessFile raf=new RandomAccessFile(file,"rw"); // 通过Socket连接文件服务器 Socket server=new Socket(InetAddress.getLocalHost(),3108); //创建网络接受流接受服务器文件数据 InputStream netIn=server.getInputStream(); InputStream in=new DataInputStream(new BufferedInputStream(netIn)); //创建缓冲区缓冲网络数据 byte[] buf=new byte[2048]; int num=in.read(buf); while(num!=(-1)){//是否读完所有数据 raf.write(buf,0,num);//将数据写往文件 raf.skipBytes(num);//顺序写文件字节 num=in.read(buf);//继续从网络中读取文件 } in.close(); raf.close(); } } 归结以上代码: 服务器 
    客户端 1. 服务器从本地文件系统读取文件 2. 服务器创建网络服务连接 3. 服务器提供数据包装器 4. 服务器将本地文件写入数据包装器 5. 服务器通过包装器写入到网络 
    1. 客户端建立新文件准备存储来自网络的数据 2. 客户端连接服务器 3. 客户端通过网络接受服务器数据并进行数据解包 4. 客户端将数据写入缓冲区 5. 客户端从缓冲区把数据写入客户本地文件 
    总结: 事实上java的开发环境为我们提供大多数的编程接口,为我们简化了开发工作量.我们通过java的IO接口所提供的文件,数据包装器等接口非常方便的解决了我们上面的开发工作量.同时在java的net接口所提供的套接字也使得基于连接的数据接受和发送成为非常容易的工作.