服务器实现处理GET和POST

时间:2021-11-03 17:55:58
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpServer {

public static void doGet(String message,OutputStream op) throws IOException{



//显示报文
System.out.println(message);

Pattern p
= Pattern.compile("/?a=(.+) HTTP");

Matcher page
= p.matcher(message);

String result
= "";


while(page.find()){

//System.out.println(page.group(1));
result = page.group(1);



}

result
= URLDecoder.decode(result, "GBK");




//关键在于utf-8
OutputStreamWriter osw = new OutputStreamWriter(op,"utf-8");



osw.write(
"HTTP/1.1 200 OK\r\n");
osw.write(
"Server: Apache-Coyote/1.1\r\n");
//osw.write("Set-Cookie: JSESSIONID=03493794995CE31A0F131787B6C6CBB2; Path=/; HttpOnly\r\n");
osw.write("Content-Type: text/html;charset=UTF-8\r\n");
osw.write(
"Transfer-Encoding: chunked\r\n");




osw.write(
"Date: Tue, 19 May 2015 02:48:27 GMT\r\n");
osw.write(
"\r\n");
osw.write(
"c9\r\n");
osw.write(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
osw.write(
"<HTML>\r\n");
osw.write(
" <HEAD><TITLE>A Servlet</TITLE></HEAD>\r\n");
osw.write(
" <BODY>\r\n");


//osw.write(" This is myServ, using the GET method\r\n");
osw.write("你输入了 : "+result);




osw.write(
" </BODY>\r\n");
osw.write(
"</HTML>\r\n");
osw.write(
"\r\n");
//osw.write("0");
osw.write("\r\n");
osw.write(
"\r\n");
osw.flush();
osw.close();
op.flush();
op.close();




}

public static void doPost(String message,OutputStream op,byte [] buf) throws IOException{

//显示报文
System.out.println(message);

//收取数据
new Thread(new Runnable(){

@Override
public void run() {

Pattern p
= Pattern.compile("filename=\"(.+)\"");

Matcher page
= p.matcher(message);

String filename
= "";


while(page.find()){

System.out.println(page.group(
1));
filename
= page.group(1);

}

File file
= new File("C:\\Users\\binbin\\Desktop\\计网作业\\ReciveFile"+"\\"+filename);
//if(!file.exists()){

try {
file.createNewFile();

FileOutputStream fop
= new FileOutputStream(file);

fop.write(buf,
556,20);

fop.close();



}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//}

}


}).start();




//关键在于utf-8
OutputStreamWriter osw = new OutputStreamWriter(op,"utf-8");



osw.write(
"HTTP/1.1 200 OK\r\n");
osw.write(
"Server: Apache-Coyote/1.1\r\n");
//osw.write("Set-Cookie: JSESSIONID=03493794995CE31A0F131787B6C6CBB2; Path=/; HttpOnly\r\n");
osw.write("Content-Type: text/html;charset=UTF-8\r\n");
osw.write(
"Transfer-Encoding: chunked\r\n");
osw.write(
"Date: Tue, 19 May 2015 02:48:27 GMT\r\n");
osw.write(
"\r\n");
osw.write(
"c9\r\n");
osw.write(
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
osw.write(
"<HTML>\r\n");
osw.write(
" <HEAD><TITLE>A Servlet</TITLE></HEAD>\r\n");
osw.write(
" <BODY>\r\n");
osw.write(
" Post Method:UpLoad OK!\r\n");
osw.write(
" </BODY>\r\n");
osw.write(
"</HTML>\r\n");
osw.write(
"\r\n");
osw.write(
"\r\n");
osw.write(
"\r\n");
osw.flush();
osw.close();
op.flush();
op.close();




}




public static void main(String[] args) throws IOException, InterruptedException {

// 服务器监听端口号
ServerSocket serverSocket = new ServerSocket(8099);


//死循环服务器一直运行
while(true){

// 等待接收请求,这是一个阻塞的方法,当请求到来的时候才会继续向下执行
Socket socket = serverSocket.accept();



// 获取请求内容 InputStream
InputStream is = socket.getInputStream();

Thread.sleep(
500);

// 返回报文 用 OutputStream
OutputStream out = socket.getOutputStream();

File file
= new File("GetMetho.txt");

FileOutputStream fop
= new FileOutputStream(file);


//获取报文
int len = 0;
byte [] buf = new byte[1024];
String message
= "";
while((len=is.read(buf))!=-1){

message
+= new String(buf,0,len);
/*
if(message.contains("favicon.ico")||message.contains("google.com:443"))
break;
*/
fop.write(buf,
0,len);
break;

}

fop.close();

//System.out.println(message);


if(message.contains("favicon.ico")||message.contains("google.com:443"))
continue;

//如果是Post
else if(message.contains("POST")){
HttpServer.doPost(message,out,buf);
}

//如果是Get
else if(message.contains("GET")){
//System.out.println(message);
HttpServer.doGet(message,out);
}

else;



is.close();





}
//end while




}

}

 

GET的html:

<html>
<body>

<form action="http://localhost:8099" method="get">
<input name="a" type="text" value="Hello" />
<input name="" type="submit" value="提交" />
</form>

</body>
</html>

 

POST的html:

<form action="http://localhost:8099" enctype="MULTIPART/FORM-DATA"

method
="POST">
<INPUT NAME="userfile1" TYPE="file">
<input type="submit" value="submit"/>
</form>

 

POST其实是水的,因为看书里面获取上传的文件内容那段代码太长了,不想写。。就直接数了一下前面报文有多少字,直接从那后面开始读了。