客户端往服务端发送图片,可以显示有.JPG,但是是0kb
public class Demo2 {
@Test
public void client() throws IOException
{
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8898);
OutputStream outputStream = socket.getOutputStream();
BufferedInputStream bis =
new BufferedInputStream(new FileInputStream(new File("F:\\exer\\01.JPG")));
byte[] buf = new byte[1024];
int len;
while((len = bis.read())!=-1)
{
outputStream.write(buf,0,len);
}
System.out.println("客户端发送成功");
bis.close();
outputStream.close();
socket.close();
}
@Test
public void server() throws IOException {
ServerSocket serverSocket = new ServerSocket(8898);
Socket accept = serverSocket.accept();
InputStream inputStream = accept.getInputStream();
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(new File("F:\\exer\\TCP03.JPG")));
byte[] buf = new byte[1024];
int len;
while((len = inputStream.read())!=-1)
{
bos.write(buf,0,len);
}
System.out.println("服务端接受成功");
bos.close();
inputStream.close();
accept.close();
serverSocket.close();
}
}
客户端运行结果:
客户端发送成功
Process finished with exit code 0
服务端运行结果:
服务端接受成功
Process finished with exit code 0