构建Java的socket实例实现发送文件并保存到指定位置功能,构建客户端程序和服务器端程序都需要的MystreamSocket类,定义继承自java Socket的sendMessage和receiveMessage方法
时间: 2024-02-11 20:05:08 浏览: 42
好的,我可以帮你解答这个问题。首先,我们需要定义一个MystreamSocket类,该类继承自Java Socket类,并且需要实现sendMessage和receiveMessage方法。
MystreamSocket类如下所示:
```java
import java.io.*;
import java.net.Socket;
public class MystreamSocket extends Socket {
public MystreamSocket(String host, int port) throws IOException {
super(host, port);
}
public void sendMessage(File file) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
OutputStream os = getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
os.flush();
os.close();
bis.close();
}
public void receiveMessage(String savePath) throws IOException {
InputStream is = getInputStream();
File file = new File(savePath);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.flush();
bos.close();
fos.close();
is.close();
}
}
```
sendMessage方法用于发送文件,接收一个File类型的参数,该方法会将文件读取为字节流,然后通过Socket的输出流将字节流发送出去。
receiveMessage方法用于接收文件,接收一个保存路径的参数,该方法会通过Socket的输入流接收字节流,然后将字节流写入到指定的文件中。
接下来我们需要编写客户端和服务器端程序来使用MystreamSocket类。
客户端程序如下所示:
```java
import java.io.File;
import java.io.IOException;
public class Client {
public static void main(String[] args) {
String host = "localhost";
int port = 8888;
File file = new File("test.txt");
try {
MystreamSocket socket = new MystreamSocket(host, port);
socket.sendMessage(file);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该客户端程序会将当前目录下的test.txt文件发送到服务器端。
服务器端程序如下所示:
```java
import java.io.IOException;
public class Server {
public static void main(String[] args) {
int port = 8888;
String savePath = "test_copy.txt";
try {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server is waiting for client...");
Socket socket = serverSocket.accept();
System.out.println("Client has connected.");
MystreamSocket mystreamSocket = new MystreamSocket(socket.getInetAddress().getHostAddress(), port);
mystreamSocket.receiveMessage(savePath);
System.out.println("File has been received.");
mystreamSocket.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该服务器端程序会监听8888端口,当有客户端连接时,会创建一个MystreamSocket对象,并且调用该对象的receiveMessage方法来接收文件,将文件保存到指定的路径中。
以上就是一个简单的Java Socket实例实现文件传输的方法,希望能对你有所帮助。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-c++](https://img-home.csdnimg.cn/images/20210720083646.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)