文章对应视频出处:
https://www.bilibili.com/video/BV15E411F75x?p=233
web演变自定义服务器
package javaweb;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 自定义服务器(浏览器输入http://127.0.0.1:8888/即可)
* Created by kevin on 2020/3/31.
*/
public class Myserver {
public static void main(String[] args) {
try {
//1.创建服务都套接字
ServerSocket serverSocket = new ServerSocket(8888);
//2.创建监听,接收客户端
Socket client = serverSocket.accept();
//3.通过客户端获得输出流
OutputStream outputStream = client.getOutputStream();
//4.创建字节输入流
InputStream inputStream = new FileInputStream("C:\\Users\\kevin\\Pictures\\temp\\百度一下,你就知道.htm");
//5.创建缓冲区,把文件读取进来
byte[] bytes = new byte[1024];
int len = 0;
while((len = inputStream.read(bytes))!=-1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
inputStream.close();
outputStream.close();
client.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}