问题遇到的现象和发生背景
fromClient.png打不开,显示找不到元素,不知道是哪里出了问题
问题相关代码,请勿粘贴截图
package com.collection.net;
/**
- Tcp服务器端
- /
public class TcpFileServer {
public static void main(String[] args) throws IOException {
}//1创建ServerSocket ServerSocket listener = new ServerSocket(9999); System.out.println("服务器已经启动..."); //2侦听,接受客户端请求 Socket socket = listener.accept(); //3获取输入流 InputStream is = socket.getInputStream(); //4边读取,边保存 FileOutputStream fos = new FileOutputStream("d:\\fromClient.png"); byte[] buf = new byte[1024]; int count = 0; while ((count= is.read(buf))!=-1){ fos.write(buf,0,count); } //5关闭 fos.close(); is.close(); socket.close(); listener.close(); System.out.println("接收完毕");
}
/**
- Tcp客户端
- /
public class TcpFileClient {
public static void main(String[] args) throws IOException {
//1创建Socket
Socket socket = new Socket("172.21.41.12",9999);
//2获取输出流
OutputStream os = socket.getOutputStream();
//3边读取文件,边发送
FileInputStream fis = new FileInputStream("d:\腾讯会议图片_20220523185932.png");
byte[] buf = new byte[1024];
int count = 0;
while ((count = fis.read())!=-1){
os.write(buf,0,count);
}
//4关闭
fis.close();
os.close();
socket.close();
System.out.println("发送完毕");
}
}