从客户端发送文件到服务端,服务端保存到本地,并发送确认消息给客户端,并关闭相应的连接
package internet;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 苗晓强
* @date 2023/8/6 16:56
* 从客户端发送文件到服务端,服务端保存到本地,并发送确认消息给客户端,并关闭相应的连接
*/
public class TCPTest2 {
@Test
public void client() {
File srcFile = new File("picture.jpg");
FileInputStream fileInputStream = null;
Socket socket = null;
OutputStream outputStream = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFile);
InetAddress inetAddress = InetAddress.getByName("192.168.126.1");
int port = 8989;
socket = new Socket(inetAddress,port);
outputStream = socket.getOutputStream();
//读写文件
int len;
byte [] buffer = new byte[1024];
while ((len = fileInputStream.read(buffer)) != -1){
outputStream.write(buffer,0,len);
}
//关闭客户端的流输出,避免服务端一直等待
socket.shutdownOutput();
System.out.println("客户端发送完毕!");
//客户端接收服务端发回的确认消息
inputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer,0,len);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (byteArrayOutputStream != null){
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server(){
int port = 8989;
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
OutputStream outputStream = null;
try {
serverSocket = new ServerSocket(port);
socket = serverSocket.accept();
//通过 socket 获取一个输入流
inputStream = socket.getInputStream();
//创建File类的实例 FileOutputStream 的实例
File descFile = new File("picture_0806.jpg");
fileOutputStream = new FileOutputStream(descFile);
//读写过程
int len;
byte [] buffer = new byte[1024];
while ((len = inputStream.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
System.out.println("服务端接收完毕");
//通过 socket 获取输出流 服务端发送确认消息给客户端
outputStream = socket.getOutputStream();
outputStream.write("你的图片很漂亮,我已收到图片".getBytes());
System.out.println("服务端发送完毕!");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}