本人不才,想用java模拟实现QQ的一些基本功能。简单的功能都差不多了,只是对文件、图片的发送没有任何思路。以前没有做过。请问谁又没有类似的源代码啊?

解决方案 »

  1.   

    包括客户端和服务器两个程序
    客户端实现包括 4 个步骤 建立 DatagramSocket 读取磁
    盘文件 利用文件内容封装数据报包 发送数据报包 详细
    如下
    1 建一个类 UClient.java 在构造方法中构建如图 1 界
    面 然后写一个 start 方法 该方法负责建立一个 Datagram
    Socket 对象 并设置好端口号
    2 给 选择文件 按钮添加一个 Action 事件 在事件方
    法中写按钮响应代码 弹出打开对话框 选择文件 根据文件
    名读取文件内容
    3 建立一个 DatagramPacket 对象 即一个数据报包 包
    的内容是读取的文件内容
    4 用 DatagramSocket 对象把数据报包发送出去
    package qqwenjian;import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetSocketAddress;
    import java.net.SocketException;import javax.swing.*;public class UClient extends JFrame implements ActionListener {
    private JButton open = new JButton("选择文件");
    private DatagramPacket dp; // 数据报包
    private DatagramSocket ds; // 数据报 Socket
    private String filename; // 保存选择的文件的名字
    private FileInputStream fis;
    // 输入流 用来读取磁盘文件
    byte[] buf = new byte[10240]; // 字节数组 存放读取的文件的数据构造方法 构建界面
    public UClient() {
    this.setSize(150, 100);
    this.setTitle("发送文件");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    this.add(open);
    open.addActionListener(this);
    this.setVisible(true);
    } public void start() {
    try {
    ds = new DatagramSocket(1234);
    // 首先启动 Socket
    } catch (SocketException e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    new UClient().start();
    } // 事件响应代码
    public void actionPerformed(ActionEvent e) {
    JFileChooser jfc = new JFileChooser();
    // 文件选择器组件
    jfc.showOpenDialog(this); // 显示打开对话框
    filename = jfc.getSelectedFile().getPath();
    // 获得选中文件路径和名字
    try {
    fis = new FileInputStream(filename);
    int c;
    while ((c = fis.read(buf)) != -1) { // 读文件 数据
    // 存入 buf 字节数组
    // 利用 buf 做数据报包
    dp = new DatagramPacket(buf, c, new InetSocketAddress("127.0.0.1", 4567));
    ds.send(dp); // 发送出去
    }
    fis.close();
    } catch (IOException e2) {
    e2.printStackTrace();
    }
    }
    }
    服务器实现也需要四个步骤 建立 DatagramSocket 接收数
    据报包 把数据报包内容写入一个临时文件 保存正式的文件
    1 新建一个 UServer.java 类 在构造方法中构建如图 1
    界面
    2 写一个 start 方法 该方法负责建立 DatagramSocket 对
    象 再建立一个 DatagramPacket 对象 然后循环接收客户端发
    来的数据报包 并写入到一个临时文件中
    3 给 保存文件 按钮添加 Aciton 事件 在事件方法中写
    按钮响应代码 打开保存对话框 得到要保存的路径和文件名
    4 把临时文件的内容读取出来 重新写入到保存对话框
    指定的文件内
    package qqwenjian;import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;public class UServer extends JFrame implements ActionListener {
    private JButton save = new JButton("保存文件");
    private DatagramPacket dp;
    private DatagramSocket ds;
    private FileOutputStream fos;
    private FileInputStream fis;
    private String filename;
    private byte[] buf = new byte[10240]; public UServer() {
    this.setSize(150, 100);
    this.setTitle("接收文件");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(new FlowLayout());
    this.add(save);
    save.addActionListener(this);
    this.setVisible(true);
    } public void start() {
    try {
    ds = new DatagramSocket(4567);
    fos = new FileOutputStream("c:\\temp.dat");
    while (true) {
    dp = new DatagramPacket(buf, buf.length);
    ds.receive(dp);
    fos.write(dp.getData());
    fos.flush();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public static void main(String[] args) {
    new UServer().start();
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == save) {
    JFileChooser jfc = new JFileChooser();
    jfc.showSaveDialog(this);
    filename = jfc.getSelectedFile().getPath();
    try {
    fos.close();
    fos = new FileOutputStream(filename);
    fis = new FileInputStream("c:\\temp.dat");
    int c;
    while ((c = fis.read(buf)) != -1) {
    fos.write(buf);
    fos.flush();
    }
    fos.close();
    fis.close();
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    }
    }
    }
      

  2.   

    简单点的代码:
    先写服务器FileServer
    package wenjian;
    import java.io.*;
    import java.net.*;
    public class FileServer { public static void main(String[] args) throws IOException {
    ServerSocket server = new ServerSocket(9999);
    Socket soc = server.accept();
    InputStream is = soc.getInputStream();
    byte[] b = new byte[1024];
    int i = is.read(b);
    FileOutputStream fos = new FileOutputStream(new File("e://Winter.jpg"));
    try{
    while(true){
    fos.write(b, 0, i);
    fos.flush();
    i = is.read(b);
    System.out.println(i);
    }
    }catch (SocketException se){
    System.out.println("文件接受完毕");
    }
    }}
    再写客户端FileClient
    package wenjian;
    import java.io.*;
    import java.net.*;
    public class FileClient { public static void main(String[] args) throws Exception, IOException {
    Socket soc = new Socket("localhost",9999);
    OutputStream os = soc.getOutputStream();
    FileInputStream fis = new FileInputStream(new File("c://Winter.jpg"));
    byte[] b = new byte[1024];
    int i = fis.read(b);
    while(i != -1){
    os.write(b, 0, i);
    os.flush();
    i = fis.read(b);
    }
    }}