JAVA实现HTTPserver端
用java socket實現了一個簡單的httpserver, 能夠處理GET, POST,以及帶一個附件的multipart類型的POST。盡管中途遇到了非常多問題, 只是通過在論壇和幾個高手交流了一下,問題都攻克了。假設你認為程序有些地方看不明確,能夠參看這個帖子:http://topic.csdn.net/u/20090625/22/59a5bfc8-a6b6-445d-9829-ea6d462a4fe6.html .
盡管解析http頭不是非常規范,本來應該用原始的字節流, 我採用了一個折衷的方案,用DataInputStream.
本代碼的有用性==0,可是能夠幫助非常好地了解http協議,然后其它的應用層協議大都如此。
假設你從來都沒有了解過http協議,建議先搜索閱讀一下,或者你還能夠用以下的代碼來簡單的看一看究竟瀏覽器和server之間都相互發送了什么數據。
MyHttpClient.java: 模擬瀏覽器的行為, 向server發送get/post請求,然后打印出server返回的消息。這樣就能夠查看當一個請求到來之后, server究竟都給瀏覽器發送了哪些消息。
package socket; import java.io.*; import java.net.*; public class MyHttpClient { public static void main(String[] args) throws Exception{ InetAddress inet = InetAddress.getByName("www.baidu.com"); System.out.println(inet.getHostAddress()); Socket socket = new Socket(inet.getHostAddress(),80); InputStream in = socket.getInputStream(); OutputStream out = socket.getOutputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); PrintWriter writer = new PrintWriter(out); writer.println("GET /home.html HTTP/1.1");//home.html是關于百度的頁面 writer.println("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*"); writer.println("Accept-Language: en-us,zh-cn;q=0.5"); writer.println("Accept-Encoding: gzip, deflate"); writer.println("Host: www.baidu.com"); writer.println("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"); writer.println("Connection: Keep-Alive"); writer.println(); writer.flush(); String line = reader.readLine(); while(line!=null){ System.out.println(line); line = reader.readLine(); } reader.close(); writer.close(); } }
MyServer.java: 模擬server端接收瀏覽器的請求,然后把整個請求的報文打印出來。程序執行之后直接用瀏覽器測試。
package socket; import java.io.*; import java.net.*; public class MyServer { public static void main(String[] args) throws IOException{ ServerSocket svrSocket = new ServerSocket(8080); while(true){ Socket socket = svrSocket.accept(); //足夠大的一個緩沖區 byte[] buf = new byte[1024*1024]; InputStream in = socket.getInputStream(); int byteRead = in.read(buf, 0, 1024*1024); String dataString = new String(buf, 0, byteRead); System.out.println(dataString); in.close(); socket.close(); } } }
主程序MyHttpServer.
package socket; import java.io.*; import java.net.*; /** * MyHttpServer 實現一個簡單的HTTPserver端,能夠獲取用戶提交的內容 * 并給用戶一個response * 由于時間的關系,對http頭的處理顯得不規范 * 對于上傳附件,臨時僅僅能解析僅僅上傳一個附件并且附件位置在第一個的情況 * 轉載請注明來自http://blog.csdn.net/sunxing007 * **/ public class MyHttpServer { //server根文件夾,post.html, upload.html都放在該位置 public static String WEB_ROOT = "c:/root"; //port private int port; //用戶請求的文件的url private String requestPath; //mltipart/form-data方式提交post的分隔符, private String boundary = null; //post提交請求的正文的長度 private int contentLength = 0; public MyHttpServer(String root, int port) { WEB_ROOT = root; this.port = port; requestPath = null; } //處理GET請求 private void doGet(DataInputStream reader, OutputStream out) throws Exception { if (new File(WEB_ROOT + this.requestPath).exists()) { //從server根文件夾下找到用戶請求的文件并發送回瀏覽器 InputStream fileIn = new FileInputStream(WEB_ROOT + this.requestPath); byte[] buf = new byte[fileIn.available()]; fileIn.read(buf); out.write(buf); out.close(); fileIn.close(); reader.close(); System.out.println("request complete."); } } //處理post請求 private void doPost(DataInputStream reader, OutputStream out) throws Exception { String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); if ("".equals(line)) { break; } else if (line.indexOf("Content-Length") != -1) { this.contentLength = Integer.parseInt(line.substring(line.indexOf("Content-Length") + 16)); } //表明要上傳附件, 跳轉到doMultiPart方法。 else if(line.indexOf("multipart/form-data")!= -1){ //得multiltipart的分隔符 this.boundary = line.substring(line.indexOf("boundary") + 9); this.doMultiPart(reader, out); return; } } //繼續讀取普通post(沒有附件)提交的數據 System.out.println("begin reading posted data......"); String dataLine = null; //用戶發送的post數據正文 byte[] buf = {}; int size = 0; if (this.contentLength != 0) { buf = new byte[this.contentLength]; while(size<this.contentLength){ int c = reader.read(); buf[size++] = (byte)c; } System.out.println("The data user posted: " + new String(buf, 0, size)); } //發送回瀏覽器的內容 String response = ""; response += "HTTP/1.1 200 OK/n"; response += "Server: Sunpache 1.0/n"; response += "Content-Type: text/html/n"; response += "Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT/n"; response += "Accept-ranges: bytes"; response += "/n"; String body = "<html><head><title>test server</title></head><body><p>post ok:</p>" + new String(buf, 0, size) + "</body></html>"; System.out.println(body); out.write(response.getBytes()); out.write(body.getBytes()); out.flush(); reader.close(); out.close(); System.out.println("request complete."); } //處理附件 private void doMultiPart(DataInputStream reader, OutputStream out) throws Exception { System.out.println("doMultiPart ......"); String line = reader.readLine(); while (line != null) { System.out.println(line); line = reader.readLine(); if ("".equals(line)) { break; } else if (line.indexOf("Content-Length") != -1) { this.contentLength = Integer.parseInt(line.substring(line.indexOf("Content-Length") + 16)); System.out.println("contentLength: " + this.contentLength); } else if (line.indexOf("boundary") != -1) { //獲取multipart分隔符 this.boundary = line.substring(line.indexOf("boundary") + 9); } } System.out.println("begin get data......"); /*以下的凝視是一個瀏覽器發送帶附件的請求的全文,全部中文都是說明性的文字***** <HTTP頭部內容略> ............ Cache-Control: no-cache <這里有一個空行,表明接下來的內容都是要提交的正文> -----------------------------7d925134501f6<這是multipart分隔符> Content-Disposition: form-data; name="myfile"; filename="mywork.doc" Content-Type: text/plain <附件正文>........................................ ................................................. -----------------------------7d925134501f6<這是multipart分隔符> Content-Disposition: form-data; name="myname"<其它字段或附件> <這里有一個空行> <其它字段或附件的內容> -----------------------------7d925134501f6--<這是multipart分隔符,最后一個分隔符多兩個-> ****************************************************************/ /** * 上面的凝視是一個帶附件的multipart類型的POST的全文模型, * 要把附件去出來,就是要找到附件正文的起始位置和結束位置 * **/ if (this.contentLength != 0) { //把全部的提交的正文,包含附件和其它字段都先讀到buf. byte[] buf = new byte[this.contentLength]; int totalRead = 0; int size = 0; while (totalRead < this.contentLength) { size = reader.read(buf, totalRead, this.contentLength - totalRead); totalRead += size; } //用buf構造一個字符串,能夠用字符串方便的計算出附件所在的位置 String dataString = new String(buf, 0, totalRead); System.out.println("the data user posted:/n" + dataString); int pos = dataString.indexOf(boundary); //下面略過4行就是第一個附件的位置 pos = dataString.indexOf("/n", pos) + 1; pos = dataString.indexOf("/n", pos) + 1; pos = dataString.indexOf("/n", pos) + 1; pos = dataString.indexOf("/n", pos) + 1; //附件開始位置 int start = dataString.substring(0, pos).getBytes().length; pos = dataString.indexOf(boundary, pos) - 4; //附件結束位置 int end = dataString.substring(0, pos).getBytes().length; //下面找出filename int fileNameBegin = dataString.indexOf("filename") + 10; int fileNameEnd = dataString.indexOf("/n", fileNameBegin); String fileName = dataString.substring(fileNameBegin, fileNameEnd); /** * 有時候上傳的文件顯示完整的文件名稱路徑,比方c:/my file/somedir/project.doc * 但有時候僅僅顯示文件的名字,比方myphoto.jpg. * 所以須要做一個推斷。 */ if(fileName.lastIndexOf("//")!=-1){ fileName = fileName.substring(fileName.lastIndexOf("//") + 1); } fileName = fileName.substring(0, fileName.length()-2); OutputStream fileOut = new FileOutputStream("c://" + fileName); fileOut.write(buf, start, end-start); fileOut.close(); fileOut.close(); } String response = ""; response += "HTTP/1.1 200 OK/n"; response += "Server: Sunpache 1.0/n"; response += "Content-Type: text/html/n"; response += "Last-Modified: Mon, 11 Jan 1998 13:23:42 GMT/n"; response += "Accept-ranges: bytes"; response += "/n"; out.write("<html><head><title>test server</title></head><body><p>Post is ok</p></body></html>".getBytes()); out.flush(); reader.close(); System.out.println("request complete."); } public void service() throws Exception { ServerSocket serverSocket = new ServerSocket(this.port); System.out.println("server is ok."); //開啟serverSocket等待用戶請求到來,然后依據請求的類別作處理 //在這里我僅僅針對GET和POST作了處理 //當中POST具有解析單個附件的能力 while (true) { Socket socket = serverSocket.accept(); System.out.println("new request coming."); DataInputStream reader = new DataInputStream((socket.getInputStream())); String line = reader.readLine(); String method = line.substring(0, 4).trim(); OutputStream out = socket.getOutputStream(); this.requestPath = line.split(" ")[1]; System.out.println(method); if ("GET".equalsIgnoreCase(method)) { System.out.println("do get......"); this.doGet(reader, out); } else if ("POST".equalsIgnoreCase(method)) { System.out.println("do post......"); this.doPost(reader, out); } socket.close(); System.out.println("socket closed."); } } public static void main(String args[]) throws Exception { MyHttpServer server = new MyHttpServer("c:/root", 8080); server.service(); } }
測試文件post.html, upload.html都放在上面程序定義的WEB_ROOT以下。
post.html:處理普通的post請求
<html> <head> <title>test my server</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <p>upload</p> 來自http://blog.csdn.net/sunxing007<br> <form name="UploadForm" method="post" action="http://localhost:8080/"> <input type="text" name="myname" /><br> <select name="myage"> <option value="18">18</option> <option value="20">20</option> <option value="22">22</option> </select><br> <input type="submit"value="Sutmit"> </form> </body> </html>
upload.html:測試帶附件的post請求
<head> <title>my page</title> <style> table{ border-collapse: collapse; } </style> </head> <body> 來自http://blog.csdn.net/sunxing007<br> <form action='http://localhost:8080/' method='post' enctype='multipart/form-data'> file: <input type='file' name='myfile' /><br> <input type='submit' /> </form> </body> </html>
一切準備妥當,而且MyHttpServer執行之后, 在瀏覽器輸入http://localhost:8080/post.html和http://localhost:8080/upload.html就可以進行測試.
轉載請注明來自http://blog.csdn.net/sunxing007
總結
以上是生活随笔為你收集整理的JAVA实现HTTPserver端的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SVN关于忽略xcuserdata目录
- 下一篇: UltraEdit正则表达式介绍及实例