java swing 界面显示,文件选择按钮并且选择后显示在文本框,再添加文件发送按钮 已socket形式发送到指定IP及端口号
时间: 2024-05-13 11:20:54 浏览: 84
以下是一个简单的Java Swing界面,其中包括一个文件选择按钮、一个文本框和一个发送按钮。一旦用户选择一个文件,文件路径将显示在文本框中。当用户点击发送按钮时,文件将作为数据包通过Socket发送到指定的IP地址和端口号。
```
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
public class FileSender extends JFrame {
private JTextField ipField, portField, fileField;
private JButton selectButton, sendButton;
public FileSender() {
initComponents();
}
private void initComponents() {
setTitle("File Sender");
// 创建IP地址和端口号输入框
ipField = new JTextField("127.0.0.1", 10);
portField = new JTextField("8080", 5);
// 创建文件选择按钮和文件路径文本框
selectButton = new JButton("选择文件");
selectButton.addActionListener(new SelectAction());
fileField = new JTextField(20);
fileField.setEditable(false);
// 创建发送按钮
sendButton = new JButton("发送");
sendButton.addActionListener(new SendAction());
// 创建布局面板
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
c.insets = new Insets(5, 5, 5, 5);
panel.add(new JLabel("IP地址:"), c);
c.gridx++;
panel.add(ipField, c);
c.gridx++;
panel.add(new JLabel("端口号:"), c);
c.gridx++;
panel.add(portField, c);
c.gridx = 0;
c.gridy++;
panel.add(new JLabel("文件路径:"), c);
c.gridx++;
panel.add(fileField, c);
c.gridx++;
panel.add(selectButton, c);
c.gridx = 0;
c.gridy++;
c.gridwidth = 2;
panel.add(sendButton, c);
// 将布局面板添加到窗口中
getContentPane().add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private class SelectAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 创建文件选择器
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("所有文件", "*");
chooser.setFileFilter(filter);
// 显示文件选择器对话框
int returnVal = chooser.showOpenDialog(FileSender.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
// 获取所选文件的路径
File file = chooser.getSelectedFile();
fileField.setText(file.getAbsolutePath());
}
}
}
private class SendAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 获取IP地址和端口号
String ip = ipField.getText();
int port = Integer.parseInt(portField.getText());
// 获取文件路径
String filePath = fileField.getText();
if (filePath.equals("")) {
JOptionPane.showMessageDialog(FileSender.this, "请选择要发送的文件", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 创建Socket并连接到服务器
try (Socket socket = new Socket(ip, port)) {
// 创建输入输出流
InputStream in = new FileInputStream(filePath);
OutputStream out = socket.getOutputStream();
// 发送文件
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
// 关闭输入输出流
in.close();
out.close();
JOptionPane.showMessageDialog(FileSender.this, "文件发送成功", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException ex) {
JOptionPane.showMessageDialog(FileSender.this, "文件发送失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FileSender().setVisible(true);
}
});
}
}
```
阅读全文