Java课程设计博客(团队)

Java课程设计博客(团队)

1. 团队/项目名称

  • 使用JAVA实现简易HTTP服务器

2. 团队成员

  • 组长:林一心

  • 组员:张杭镖

3. 项目git地址

https://github.com/orange666/HTTPServer/

4. 项目git提交记录截图

5. 项目功能架构图与主要功能流程图

(1)功能框架图

(2)功能流程图

6. 项目运行截图

7. 项目关键代码

private void doGet(DataInputStream in , OutputStream out, String reqLine) throws IOException{


	String [] tokens = reqLine.split("\s+");  //数组储存分隔符,正则表达式,\s空格
	String filePath = tokens[1];      //文件地址
	String fileName = filePath;       //文件名
	/*
	if(filePath.indexOf('?') != -1){  //是否包含键值对(?开头,URL规范)
		String fileNm = fileName.substring(0,fileName.indexOf('?')); //判定url是否有键值对
		String parameters = fileName.substring(fileName.indexOf('?')+1, fileName.length());//提取?以后键值对内容
		
		String [] pars = parameters.split("&"); //分隔符
		HashMap<String ,ArrayList<String>> parameterMap = new HashMap<String ,ArrayList<String>>();
		for(String s : pars){
			String[] kv = s.split("=");
			String key = null;
			String value = null;
			if(kv.length == 2 ){
				key = kv[0] ;
				value = kv[1];
			}else if(kv.length == 1 ){
				key = kv[0] ;
				value = "";
			}else{
				continue ;
			}
			ArrayList<String> values = parameterMap.get(key);
			if(values == null){
				values = new ArrayList<String>();
				values.add(value);
				parameterMap.put(key, values);
			}else{
				values.add(value);
			}
		}			
			fileName = fileNm;
			doGetWithParameter( in ,  out, fileName , parameterMap);							
		    return ;
	}else{
		if(fileName.endsWith("/")){
			fileName += indexFileNname;//资源名
		}
	}	
	*/	
	if(fileName.endsWith("/")){
		fileName += indexFileNname;//资源名
	}
	String contentTpye = URLConnection.getFileNameMap().getContentTypeFor(fileName);//判断需求类型,得到请求类型,比如图片、文字等
	String version = null ;  //版本号 
	if(tokens.length > 2){
		version = tokens[2]; //提取版本号
	}
	Writer outPut = new OutputStreamWriter(out);  
	File theFile = new File(rootDirectory,fileName.substring(1, fileName.length()));  //index文件名,从1即/开始
	if(theFile.canRead() && theFile.getCanonicalPath().startsWith(rootDirectory.getPath())&& !theFile.isDirectory()){//判断是否只读/以已设置目录开头/是否合法路径
		byte[] theData = Files.readAllBytes(theFile.toPath()); //字节数组,存文件内容
		if(version.startsWith("HTTP/")){
			senderHeader(outPut,"Http/1.0 200 OK",contentTpye,theData.length); //向客户端输出报文
		}
		out.write(theData);//111
		out.flush();
	}else{
		String body = new StringBuilder("HTTP error 404 :File Not Found
").toString();//
		if(version.startsWith("HTTP/")){
			senderHeader(outPut,"Http/1.0 404 File Not Found","text/html;charset=utf-8",body.length());
		}
		outPut.write(body);
		outPut.flush();
		
	}
}

private void doPost(DataInputStream in , OutputStream out, String reqLine) throws IOException{ 
			
	
	String [] tokens = reqLine.split("\s+");
		String reqPath = tokens[1] ;
		HashMap<String ,String> headers = new HashMap<String ,String>();
		in.skip(1);
		String theLine = in.readLine();
		//System.out.println(theLine);
		while (theLine != null) {
			System.out.println(theLine);
			if ("".equals(theLine)) {
				break;
			}
			String [] headKV = theLine.split(": ");
			headers.put(headKV[0], headKV[1]);
			theLine = in.readLine();
		//System.out.println("文字:"+theLine);
	}

   /*分析表单编码类型*、
   Set<Entry<String, String>>entrys = headers.entrySet();
	for(Entry<String, String> h : entrys){
		if(h.getKey().equalsIgnoreCase("Content-Type")){
			if(h.getValue().contains("application/x-www-form-urlencoded")){
				doPostWithformUrlencoded( in ,  out , headers  );
				return ;
			}else if(h.getValue().contains("multipart/form-data")){
				//doPostWithMultiPart( in ,  out , headers );
				return ;
			}
		}			
	}
	Writer outPut = new OutputStreamWriter(out);
	String body = new StringBuilder("HTTP error 501 :Not Implemented 
").toString();
	String version = null ;
	if(tokens.length > 2){
		version = tokens[2];
	}
	if(version.startsWith("HTTP/")){
		senderHeader(outPut,"Http/1.0 501 Not Implemented ","text/html;charset=utf-8",body.length());
	}
	outPut.write(body);
	outPut.flush();
	
}

/*默认application/x-www-form-urlencoded  */
void doPostWithformUrlencoded(DataInputStream in , OutputStream out ,HashMap<String ,String> headers ) throws IOException{
	Writer outPut = new OutputStreamWriter(out);		
	int contentLength = 0 ;
	 Set<Entry<String, String>>entrys = headers.entrySet();
		for(Entry<String, String> h : entrys){
			if(h.getKey().equalsIgnoreCase("Content-Length")){
				contentLength =  Integer.parseInt(h.getValue());
				break ;
			}
			
		}
		if(contentLength != 0){
			
			byte []bodyContent = new byte[contentLength];
			int totalRed = 0 ;
			int size = 0 ;
			while(totalRed < contentLength){
			  size = 	in.read(bodyContent, totalRed, contentLength-totalRed) ;
			  totalRed += size;
			}
			String parameters = new String(bodyContent);
			String [] pars = parameters.split("&");
			HashMap<String ,ArrayList<String>> parameterMap = new HashMap<String ,ArrayList<String>>();
			for(String s : pars){
				String[] kv = s.split("=");
				String key = null;
				String value = null;
				if(kv.length == 2 ){
					key = kv[0] ;
					value = kv[1];
				}else if(kv.length == 1 ){
					key = kv[0] ;
					value = "";
				}else{
					continue ;
				}
				ArrayList<String> values = parameterMap.get(key);
				if(values == null){
					values = new ArrayList<String>();
					values.add(value);
					parameterMap.put(key, values);
				}else{
					values.add(value);
				}
			}
			StringBuilder body = new StringBuilder();
			body.append("<html><head><title>Test post with formUrlencoded</title></head><body><p>Post is ok</p></body></html>");			
			senderHeader(outPut,"Http/1.0 200 OK","text/html;charset=utf-8",body.length());
			outPut.write(body.toString());
			outPut.flush();
		}
}

8. 尚待改进或者新的想法

上传文件及下载文件功能

9. 团队成员任务分配,团队成员课程设计博客链接

团队成员 任务分配 博客链接
林一心(组长) 响应POST请求 http://www.cnblogs.com/wkfg/p/7063085.html
张杭镖 响应GET请求 http://www.cnblogs.com/ghnb/p/7063321.html
原文地址:https://www.cnblogs.com/wkfg/p/7063081.html