minicat 1.0我们实现了返回固定的字符串"Hello minicat"。
minicat 2.0需求:
封装Request和Response对象,返回html静态资源文件。
封装Request对象
想要封装Request对象,得要先知道请求信息里面有哪些数据,所以我们先打印请求信息。
public void start() throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("---->>>minicat start on port:"+port);
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
int count = 0;
while (count == 0) {
count = inputStream.available();
}
byte[] bytes = new byte[count];
inputStream.read(bytes);
System.out.println("====>>> 请求信息:"+new String(bytes));
socket.close();
}
}
打印信息如下:
====>>> 请求信息:GET / HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: navigate
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7
根据打印出来的请求信息,找到重点要封装的地方 “GET / HTTP/1.1” 。
- “GET”请求方式需要封装,因为后面会根据不同的请求方式找不同的servlet。
- GET后面的“/”也需要封装,这代表的是请求路径。
- “HTTP/1.1”这里不需要要封装,tomcat默认就用HTTP/1.1。
定义Request对象
经过分析,我们需要封装请求方式和请求路径,所以Request定义如下:
public class Request {
private String method;
private String url;
private InputStream inputStream;
}
在Request实体中增加构造方法,给method和url赋值
public Request() {
}
public Request(InputStream inputStream) throws IOException {
this.inputStream = inputStream;
int count = 0;
while (count == 0) {
count = inputStream.available();
}
byte[] bytes = new byte[count];
inputStream.read(bytes);
String inputStr = new String(bytes);
String firstLineStr = inputStr.split("\\n")[0];
String[] strings = firstLineStr.split(" ");
this.method = strings[0];
this.url = strings[1];
}
封装Response对象
Response对象中需要做的事情,根据url获取静态资源绝对路径,进一步根据绝对路径获取静态资源,最终通过输出流输出
获取静态资源文件的绝对路径
public class StaticResourceUtil {
public static String getAbsolutePath(String path) {
String absolutePath = StaticResourceUtil.class.getResource("/").getPath();
return absolutePath.replaceAll("\\\\","/")+path;
}
}
根据绝对路径获取静态资源
File file = new File(absoluteResourcePath);
通过输出流输出
public class StaticResourceUtil {
public static void outputStaticResource(InputStream inputStream, OutputStream outputStream) throws IOException {
int count = 0;
while (count == 0) {
count = inputStream.available();
}
int resourceSize = count;
outputStream.write(HttpProtocolUtil.getHttpHeader200(resourceSize).getBytes());
long written = 0;
int byteSize = 1024;
byte[] bytes = new byte[byteSize];
while (written < resourceSize) {
if (written + byteSize > resourceSize) {
byteSize = (int) (resourceSize - written);
bytes = new byte[byteSize];
}
inputStream.read(bytes);
outputStream.write(bytes);
written+=byteSize;
}
}
}
Response完整代码如下:
public class Response {
private OutputStream outputStream;
public Response() {
}
public Response(OutputStream outputStream) {
this.outputStream = outputStream;
}
public void output (String content) throws IOException {
outputStream.write(content.getBytes());
}
public void outputHtml(String path) throws IOException {
String absoluteResourcePath = StaticResourceUtil.getAbsolutePath(path);
File file = new File(absoluteResourcePath);
if (file.exists() && file.isFile()) {
StaticResourceUtil.outputStaticResource(new FileInputStream(file),outputStream);
}else{
output(HttpProtocolUtil.getHttpHeader404());
}
}
}
V2.0测试
启动类中更改如下
public void start() throws IOException {
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("---->>>minicat start on port:"+port);
while (true) {
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
Request request = new Request(inputStream);
Response response = new Response(socket.getOutputStream());
response.outputHtml(request.getUrl());
socket.close();
}
}
public static void main(String[] args) {
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap.start();
} catch (IOException e) {
e.printStackTrace();
}
}
在resources目录下增加index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>静态资源</title>
</head>
<body>
Hello minicat 静态资源
</body>
</html>
启动效果如下:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)