由于题目没有给出具体的图形交互页面要求,因此以下仅为示例代码,仅实现了一个简单的文件选择和传输进度展示的图形界面:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;

public class FileTransferClient extends JFrame implements ActionListener {

    private static final String SERVER_IP = "219.218.21.89"; // 服务端IP
    private static final int SERVER_PORT = 8899; // 服务端端口

    private Socket client;
    private FileInputStream fis;
    private DataOutputStream dos;

    private JLabel fileLabel;
    private JButton fileButton;
    private JButton sendButton;
    private JProgressBar progressBar;

    public FileTransferClient() {
        super("文件传输Client端");

        // 初始化界面组件
        fileLabel = new JLabel("请选择文件");
        fileButton = new JButton("选择文件");
        sendButton = new JButton("发送");
        progressBar = new JProgressBar(0, 100);

        // 注册事件监听器
        fileButton.addActionListener(this);
        sendButton.addActionListener(this);

        // 添加组件到界面
        Container container = getContentPane();
        container.setLayout(new BorderLayout());
        container.add(fileLabel, BorderLayout.WEST);
        container.add(fileButton, BorderLayout.CENTER);
        container.add(sendButton, BorderLayout.EAST);
        container.add(progressBar, BorderLayout.SOUTH);

        // 设置界面大小和位置
        setSize(400, 100);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    /**
     * 选择文件事件处理
     * @param e
     */
    private void selectFile(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog(FileTransferClient.this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            fileLabel.setText(file.getName());
        }
    }

    /**
     * 发送文件事件处理
     * @param e
     */
    private void sendFile(ActionEvent e) {
        try {
            File file = new File(fileLabel.getText());
            if (file.exists()) {
                // 建立连接
                client = new Socket(SERVER_IP, SERVER_PORT);

                // 初始化数据流
                fis = new FileInputStream(file);
                dos = new DataOutputStream(client.getOutputStream());

                // 文件名和长度
                dos.writeUTF(file.getName());
                dos.flush();
                dos.writeLong(file.length());
                dos.flush();

                // 开始传输文件
                System.out.println("======== 开始传输文件 ========");
                byte[] bytes = new byte[1024];
                int length = 0;
                long progress = 0;
                while ((length = fis.read(bytes, 0, bytes.length)) != -1) {
                    dos.write(bytes, 0, length);
                    dos.flush();
                    progress += length;
                    progressBar.setValue((int) (100 * progress / file.length()));
                }
                System.out.println();
                System.out.println("======== 文件传输成功 ========");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
                if (dos != null)
                    dos.close();
                if (client != null)
                    client.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 按钮事件处理
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == fileButton) {
            selectFile(e);
        } else if (e.getSource() == sendButton) {
            sendFile(e);
        }
    }

    /**
     * 入口
     * @param args
     */
    public static void main(String[] args) {
        new FileTransferClient();
    }

}
``
修改下列代码增加图形交互页面:import javaioDataOutputStream;import javaioFile;import javaioFileInputStream;import javanetSocket; 文件传输Client端br public class FileTransferClient extends Socket private static final

原文地址: http://www.cveoy.top/t/topic/flYr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录