java swing 将选中文件发送到指定端口和ip
时间: 2023-11-22 20:59:16 浏览: 38
可以使用Socket实现,具体步骤如下:
1. 创建Socket实例,指定目标IP和端口号
2. 获取选中文件的路径
3. 创建输入输出流,将选中文件读入程序,并通过Socket发送到目标IP和端口号
4. 关闭Socket、输入输出流
代码示例:
```
try {
// 创建Socket实例,指定目标IP和端口号
Socket socket = new Socket("目标IP", 目标端口号);
// 获取选中文件的路径
File file = new File("选中文件路径");
// 创建输入输出流,将选中文件读入程序,并通过Socket发送到目标IP和端口号
FileInputStream fis = new FileInputStream(file);
OutputStream os = socket.getOutputStream();
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) != -1) {
os.write(buf, 0, len);
}
// 关闭输入输出流和Socket
fis.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
```
希望能够帮你解决问题。
相关问题
java swing 显示ip地址输入框 端口号输入框实现文件传输
以下是一个简单的Java Swing程序,用于实现文件传输的IP地址输入框和端口号输入框:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class FileTransfer extends JFrame implements ActionListener {
private JTextField ipField;
private JTextField portField;
private JButton sendButton;
private JButton chooseButton;
private JFileChooser fileChooser;
public FileTransfer() {
setTitle("File Transfer");
setSize(400, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(2, 2));
JLabel ipLabel = new JLabel("IP Address:");
ipField = new JTextField();
JLabel portLabel = new JLabel("Port:");
portField = new JTextField();
topPanel.add(ipLabel);
topPanel.add(ipField);
topPanel.add(portLabel);
topPanel.add(portField);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(1, 2));
chooseButton = new JButton("Choose File...");
chooseButton.addActionListener(this);
centerPanel.add(chooseButton);
sendButton = new JButton("Send File");
sendButton.addActionListener(this);
centerPanel.add(sendButton);
add(centerPanel, BorderLayout.CENTER);
fileChooser = new JFileChooser();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == chooseButton) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// Do something with the file
}
} else if (e.getSource() == sendButton) {
String ipAddress = ipField.getText();
int portNumber = Integer.parseInt(portField.getText());
File file = fileChooser.getSelectedFile();
try {
Socket socket = new Socket(ipAddress, portNumber);
OutputStream outputStream = socket.getOutputStream();
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileInputStream.close();
outputStream.close();
socket.close();
JOptionPane.showMessageDialog(this, "File sent successfully!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Error sending file: " + ex.getMessage());
}
}
}
public static void main(String[] args) {
new FileTransfer();
}
}
```
这个程序创建了一个窗口,包含一个IP地址输入框、一个端口号输入框、一个“Choose File...”按钮和一个“Send File”按钮。当用户点击“Choose File...”按钮时,程序打开一个文件选择器,允许用户选择要发送的文件。当用户点击“Send File”按钮时,程序使用Socket连接到指定的IP地址和端口号,并将文件发送到远程主机。
请注意,此程序仅是一个示例,并不包含完整的错误处理和安全功能。在实际应用中,您需要添加更多的错误处理和安全检查,以确保文件传输的安全和可靠。
java swing 界面显示,文件选择按钮并且选择后显示在文本框,再添加文件发送按钮 已socket形式发送到指定IP及端口号
以下是一个简单的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);
}
});
}
}
```