package com.yq.day75.tcp;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 3307235355@qq.com
* @description
* @since 2022/07/06 08:30
* 浏览器显示页面
*/
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[4096];
int read = inputStream.read(bytes);
System.out.println("接收到的消息" + new String(bytes, 0, read));
//返回消息
OutputStream outputStream = socket.getOutputStream();
// String s = "HTTP/1.1 200 OK\r\n" +
// "Content-type:text/html\r\n\r\n"+"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + // 解决乱码问题
// "<style>body{font-family:\"黑体\"}</style>" +
// "欢迎来到小蓝枣的博客<br>" +
// "Welcome to xiaolanzao's blog!";
FileReader fileReader = new FileReader("src\\com\\yq\\day75\\tcp\\index.html");
char[] chars = new char[1024];
int readCount;
while ((readCount = fileReader.read(chars)) != -1) {
outputStream.write(new String(chars, 0, readCount).getBytes());
System.out.println(new String(chars, 0, readCount));
}
fileReader.close();
socket.close();
serverSocket.close();
}
}
html文件
HTTP/1.1 200 OK\r\n
Content-type:text/html
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>你好</title>
</head>
<body>
<p>王道训练营java43期</p>
<a href="https://img2.baidu.com/it/u=4249630208,1769413815&fm=253&fmt=auto&app=138&f=JPEG?w=806&h=480">图片点击</a>
<img src="https://img2.baidu.com/it/u=4249630208,1769413815&fm=253&fmt=auto&app=138&f=JPEG?w=806&h=480">
</body>
</html>