/**
* 模拟web服务器,能处理http协议
* 1: http协议的定义 content-type
* 4个字节 0001 男,0000 女
* 2: 网络编程
* 客户端,服务器端
*
*
* 场景:假装一个web服务器
* bio编程,blocking
*/
/**
* 模拟web服务器,能处理http协议
* 1: http协议的定义 content-type
* 4个字节 0001 男,0000 女
* 2: 网络编程
* 客户端,服务器端
*
*
* 场景:假装一个web服务器
* bio编程,blocking
*/
public class Test {
public static void main(String[] args) throws Exception {
ServerSocket service=new ServerSocket();
InetSocketAddress port = new InetSocketAddress(8848);
service.bind(port);
Socket accept = service.accept();
InputStream is = accept.getInputStream();
OutputStream os = accept.getOutputStream();
byte[]bytes=new byte[1024];
int read = is.read(bytes);
String s = new String(bytes, 0, read);
System.out.println(cutString(s, "/", "HTTP"));
// String[] split = s.split("/.*HTTP");
// for (String s1 : split) {
// System.out.print(s1);
// }
// File file = new File("246859564411688080.jpg");
//
// BufferedReader fileReader = new BufferedReader(new FileReader(file));
//if
String s6="WANGYU666";
Integer status=200;
String ok="OK";
String copy="HTTP/1.1";
String contentType="Content-Type: text/html; charset=utf-8";
//String contentType="Content-Type: image/jpeg";
//image
//String s1 = fileReader.readLine();
String text="WANGYU666";
String s1="C:Users\\Lara\\Desktop\\中国地图.jpg";
//String context="<img"+"src="+"\""+s1+"\""+">";
String context="<h1>"+text+"</h1>";
String all=copy+" "+status+" "+ok+"\r\n"+contentType+"\r\n"+"\r\n"
+context;
os.write(all.getBytes());
//os.write(s.getBytes());
os.flush();
}
public static String cutString(String str, String start, String end) {
if (str.isBlank()) {
return str;
}
String reg= start + "(.*)" + end;// "/.*HTTP"
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
str = matcher.group(1);
}
return str;
}
/**
* 1.如果字符串中含有多个#或多个$,使用以第一个#为起始或以最后一个$为结束。
*
* 2.'$'或'.'等特殊字符要转义
*/
}