http协议------费了番脑筋

当我们向服务器提交请求的时候,一般分为post和get,get是将数据通过编码放在url中传输,而且仅仅限定是文本,所以当我们想要解析的时候,只要提取URL在进行字符串的分解就可以实现,但是当我们使用post提交的时候,数据是放在数据区的,接下来我们来看看,什么是数据区

POST / HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 339
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: file://
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXRRcAQBkcqSNCep4
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
<span style="background-color: rgb(204, 204, 204);">Content-Disposition: form-data; name="music"; filename="1.txt"
Content-Type: text/plain

11111111111
------WebKitFormBoundaryXRRcAQBkcqSNCep4
Content-Disposition: form-data; name="file"; filename="2.txt"
Content-Type: text/plain

2222222222222</span>
<span style="background-color: rgb(192, 192, 192);">------WebKitFormBoundaryXRRcAQBkcqSNCep4--</span>
oundary=----WebKitFormBoundaryXRRcAQBkcqSNCep4
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
这是我上传文件的一个实例程序的结果,着重注意的是加灰色的地方,其实也可以理解为键值对

再来看看我们的html:

    <form action='http://localhost:80/' method='post' enctype='multipart/form-data'>  
  <span style="white-space:pre">	</span>file: <input type='file' name='music' /><br>
  <span style="white-space:pre">	</span>file: <input type='file' name='file' /><br>
    <span style="white-space:pre">	</span><input type='submit' />  
    </form> 
这个会使浏览器发送数据,如果不加,浏览器则当其位一个普通的form表单

ming=%B5%DA%D2%BB%B8%F6input%B2%CE%CA%FD&wenjian=1.txt&submit=%CC%E1%BD%BB
不会把他当作一个带附件的表单给上传

最后附上一段迷你的http服务器的小例子:

package socket;

import java.io.*;
import java.net.*;

/**
 * MyHttpServer 实现一个简单的HTTP服务器端,可以获取用户提交的内容 并给用户一个response
 * 因为时间的关系,对http头的处理显得不规范 对于上传附件,暂时只能解析只上传一个附件而且附件位置在第一个的情况
 * 转载请注明来自http://blog.csdn.net/sunxing007
 * **/
public class MyHttpServer {
	// 服务器根目录,post.html, upload.html都放在该位置
	public static String WEB_ROOT = "c:/root";
	// 端口
	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()) {
			// 从服务器根目录下找到用户请求的文件并发送回浏览器
			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();
			System.out.println(line);
			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();
			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();
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值