Java 文件传输客户端代码详解:每一行代码的含义
这是一个文件传输的客户端程序。下面是每一句代码的含义:
package 文件传送器;
声明文件所在的包名。
import java.awt.BorderLayout; 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; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField;
导入需要使用的类。
public class FileTransferClient extends JFrame {
定义一个名为 FileTransferClient 的类,继承自 JFrame 类。
private static final long serialVersionUID = 1L;
定义一个序列化 ID。
private static final String SERVER_IP = '10.41.104.29'; // 服务端IP private static final int SERVER_PORT = 8899; // 服务端端口
定义服务器的 IP 地址和端口号。
private Socket client; private FileInputStream fis; private DataOutputStream dos;
声明客户端的 Socket、FileInputStream 和 DataOutputStream 对象。
private JPanel panel; private JTextField textField; private JButton chooseButton; private JButton sendButton;
声明客户端的面板、文本框和两个按钮。
public FileTransferClient() {
定义 FileTransferClient 类的构造方法。
super('文件传送Client端'); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 100);
调用 JFrame 类的构造方法,设置标题、关闭操作和窗口大小。
panel = new JPanel(); textField = new JTextField(20); chooseButton = new JButton('选择文件'); sendButton = new JButton('发送文件');
创建面板、文本框和两个按钮。
chooseButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(panel); if (result == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); textField.setText(file.getAbsolutePath()); } } });
为选择文件按钮添加 ActionListener,实现选择文件的功能。
-
`sendButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String fileName = textField.getText(); if (fileName.isEmpty()) { JOptionPane.showMessageDialog(panel, '请选择要发送的文件'); return; } File file = new File(fileName); if (!file.exists()) { JOptionPane.showMessageDialog(panel, '文件不存在,请重新选择'); return; } 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; System.out.print('| ' + (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(); } }} });`
为发送文件按钮添加 ActionListener,实现发送文件的功能。
panel.add(textField); panel.add(chooseButton); panel.add(sendButton); add(panel, BorderLayout.CENTER); setVisible(true);
将文本框和两个按钮添加到面板中,将面板添加到 JFrame 中,并显示 JFrame。
public static void main(String[] args) { new FileTransferClient(); }
定义程序的入口,创建 FileTransferClient 类的对象。
原文地址: https://www.cveoy.top/t/topic/ocrF 著作权归作者所有。请勿转载和采集!