Java 文件传输客户端:添加交互页面实现
Java 文件传输客户端:添加交互页面实现
本文将介绍如何为 Java 文件传输客户端添加交互页面,使用 Swing 库实现简单的文件选择和传输功能,方便用户操作。
代码修改
- 在
sendFile()方法中,将文件路径改为从交互页面中获取的路径:
public void sendFile(String filePath) throws Exception {
try {
File file = new File(filePath);
if(file.exists()) {
fis = new FileInputStream(file);
dos = new DataOutputStream(client.getOutputStream());
// 省略部分代码...
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 省略部分代码...
}
}
- 创建一个
JFrame类,添加一个JPanel,并在JPanel中添加一个JTextField和一个JButton,用于输入文件路径和触发文件传输。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class FileTransferClientUI extends JFrame {
private static final String SERVER_IP = '219.218.21.89'; // 服务端IP
private static final int SERVER_PORT = 8899; // 服务端端口
private JPanel jPanel;
private JTextField jTextField;
private JButton jButton;
public FileTransferClientUI() {
setTitle('文件传输Client端');
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 120);
// 创建一个JPanel,并在其中添加一个JTextField和一个JButton
jPanel = new JPanel();
getContentPane().add(jPanel, BorderLayout.CENTER);
jTextField = new JTextField();
jTextField.setColumns(30);
jPanel.add(jTextField);
jButton = new JButton('传输文件');
jPanel.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filePath = jTextField.getText();
if (filePath != null && filePath.length() > 0) {
try {
FileTransferClient client = new FileTransferClient();
client.sendFile(filePath);
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, '请输入文件路径');
}
}
});
}
public static void main(String[] args) {
// 显示交互页面
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileTransferClientUI frame = new FileTransferClientUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
- 运行程序,即可看到交互页面。在输入框中输入文件路径,点击“传输文件”按钮即可开始文件传输。
注意:为了简化代码,这里并未对用户输入的路径进行合法性校验,实际使用时需要自行添加。
示例代码
// FileTransferClient.java
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;
/**
* 文件传输Client端<br>
*/
public class FileTransferClient extends Socket {
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;
/**
* 构造函数<br/>
* 与服务器建立连接
* @throws Exception
*/
public FileTransferClient() throws Exception {
super(SERVER_IP, SERVER_PORT);
this.client = this;
System.out.println('Cliect[port:' + client.getLocalPort() + '] 成功连接服务端');
}
/**
* 向服务端传输文件
* @param filePath 文件路径
* @throws Exception
*/
public void sendFile(String filePath) throws Exception {
try {
File file = new File(filePath);
if(file.exists()) {
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 e) {
e.printStackTrace();
} finally {
if(fis != null)
fis.close();
if(dos != null)
dos.close();
client.close();
}
}
/**
* 入口
* @param args
*/
public static void main(String[] args) {
try {
FileTransferClient client = new FileTransferClient(); // 启动客户端连接
// client.sendFile('E:\test.txt'); // 传输文件
} catch (Exception e) {
e.printStackTrace();
}
}
}
// FileTransferClientUI.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
public class FileTransferClientUI extends JFrame {
private static final String SERVER_IP = '219.218.21.89'; // 服务端IP
private static final int SERVER_PORT = 8899; // 服务端端口
private JPanel jPanel;
private JTextField jTextField;
private JButton jButton;
public FileTransferClientUI() {
setTitle('文件传输Client端');
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 120);
// 创建一个JPanel,并在其中添加一个JTextField和一个JButton
jPanel = new JPanel();
getContentPane().add(jPanel, BorderLayout.CENTER);
jTextField = new JTextField();
jTextField.setColumns(30);
jPanel.add(jTextField);
jButton = new JButton('传输文件');
jPanel.add(jButton);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String filePath = jTextField.getText();
if (filePath != null && filePath.length() > 0) {
try {
FileTransferClient client = new FileTransferClient();
client.sendFile(filePath);
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, '请输入文件路径');
}
}
});
}
public static void main(String[] args) {
// 显示交互页面
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileTransferClientUI frame = new FileTransferClientUI();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
总结
通过添加交互页面,用户可以方便地选择要传输的文件,提高了程序的易用性。当然,实际应用中可能还需要考虑更多功能,比如文件选择对话框、进度条显示、错误处理等等。
原文地址: https://www.cveoy.top/t/topic/ocrq 著作权归作者所有。请勿转载和采集!