使用Java语言编写可用于实际网络环境的文件传送器,可指定传输的本地文件,并且文件传送器的两个传送端可工作于不同的主机上或同一主机的不同程序,实现一个文件接收的弹窗
时间: 2024-05-06 09:16:18 浏览: 8
要实现这个功能,可以使用Java中的Socket编程。以下是一个简单的实现过程:
1. 创建服务端
创建一个服务端程序,它会监听一个指定的端口,等待客户端连接。在客户端连接之后,服务端会接收客户端传来的文件,并将其保存到指定的本地路径。
```java
public class FileServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server started. Listening to the port 8888...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress().getHostAddress());
// 接收文件
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("receive.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
socket.close();
System.out.println("File received.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 创建客户端
创建一个客户端程序,它会连接到指定的服务端,并传输指定的本地文件。在传输完成之后,客户端会关闭连接。
```java
public class FileClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);
System.out.println("Connected to server.");
// 发送文件
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream("test.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
fis.close();
os.close();
socket.close();
System.out.println("File sent.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 创建文件接收的弹窗
在服务端程序中,可以通过Swing等GUI工具包创建一个弹窗,在文件接收完成之后,显示接收成功的提示消息。
```java
public class FileServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server started. Listening to the port 8888...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress().getHostAddress());
// 接收文件
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("receive.txt");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
socket.close();
System.out.println("File received.");
// 弹窗提示
JOptionPane.showMessageDialog(null, "File received successfully.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
以上就是一个简单的Java文件传输器的实现过程。需要注意的是,这个实现仅供参考,实际应用中还需要考虑一些安全性、性能等方面的问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)