基于Java的简易Http服务器--你瞅啥

上次看了一个基于Java的http服务器代码,功能很简单,但是却用到了很多学到的知识,感觉很有意思,就模仿着写了一个,一开始遇到一点儿问题(这个问题感觉比这个代码要难多了),现在基本上问题都解决了,关于那个问题,可以看 我的博客。部署到服务器上,感觉很不错,特地拿来和大家分享一下。

项目演示地址:你瞅啥

功能介绍

输入网址,可以通过浏览器访问到一张简单的图片,但是我感觉这个东西还是挺有趣的。
下面是全部的代码,直接复制下来就可以使用了,如果在本地测试的话,直接输入:localhost:10000即可。

代码

HttpServer类

package com.dragon;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpServer {
	private static ServerSocket server;
	
	/**
	 * 启动服务
	 * */
	public void start() {
	    try {
	    	server = new ServerSocket(10000);
	    	System.out.println("服务启动成功...");
	    	this.receiveRequest();
	    } catch (IOException e) {
	    	e.printStackTrace();
	    	System.out.println("服务启动失败!");
	    }
	}

	
	/**
	 * 接收请求
	 * */
	public void receiveRequest() {
		ExecutorService pool = Executors.newFixedThreadPool(10);
		while (true) {
			try {
				Socket client = server.accept();
				System.out.println("用户"+client.getInetAddress().toString()+"建立连接" + client.toString());
				pool.submit(new Connection(client));  //使用线程处理每一个请求。
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

Connection类

package com.dragon;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Connection implements Runnable {
	private static final String BLANK = " ";
	private static final String CRLF = "\r\n";
	private byte[] content;
	private byte[] header;
	private Socket client;
	
	public Connection(Socket client) {
		this.client = client;
	}
	
	@Override
	public void run() {
		//这里不对请求进行处理,只是收到请求之后会进行响应,而不管是什么请求
		//这里模拟服务器最原始的功能:请求、响应。
		this.response();
		System.out.println("线程执行结束了!");
	}
	
	/**
	 * 接收请求信息,这里只是一个简单的模拟,这里只能接收get请求。
	 * @throws IOException 
	 * */
	public String getRequestInfo(InputStream in) throws IOException {
		//只读取第一行,这是我们需要的全部内容
		StringBuilder requestLine = new StringBuilder(80);
		while (true) {
			int c = in.read();
			if (c == '\r' || c == '\n' || c == -1) break;
			requestLine.append((char)c);
		}
		return requestLine.toString();
	}
	
	/**
	 * 响应信息
	 * */
	public void response() {
		InputStream in = null;
		OutputStream out = null; 
		try {
			in = new BufferedInputStream(client.getInputStream());
			out = new BufferedOutputStream(client.getOutputStream());   //获取输出流
			String requestInfo = this.getRequestInfo(in);           //如果不读取客户端发来的数据,服务器就会出错,
			System.out.println(requestInfo);                        //因为这违反了Http协议的规则
		} catch (IOException e1) {
			e1.printStackTrace();
		}       //获取输入流
		
		
		//响应体数据
		Path filepath = Paths.get("/", "home", "Alfred", "kkk.jpeg"); //图片的路径
		File file = filepath.toFile();
		String contentType = null;  //文件的 MIME 类型
		try {
			content = Files.readAllBytes(file.toPath());           //使用 Files 工具类,一次性读取文件
			contentType = Files.probeContentType(file.toPath());   //获取文件的 MIME 类型
			long length = file.length();                           //获取文件字节长度
			header = this.getHeader(contentType, length);          // 填充响应头
 		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			out.write(header);        //写入Http报文头部部分
			out.write(content);       //写入Http报文数据部分
			out.flush();              //刷新输出流,确保缓冲区内数据已经发送完成
			
			System.out.println("报文总大小(字节):" + (header.length + content.length));
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("客户断开连接或者发送失败!");
		} finally {
			try {
				if (client != null) {
					client.close();
				}
				System.out.println("请求结束了");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
	
	//响应头
	private byte[] getHeader(String contentType, long length) {
		return new StringBuilder()
				.append("HTTP/1.0").append(BLANK).append(200).append(BLANK).append("OK").append(CRLF)  // 响应头部
				.append("Server:"+"CrazyDragon").append(CRLF)
				.append("My_Love_For_You:").append("I love you yesterday and today!").append(CRLF)  //添加一个自定义的 Http Header,虽然已经禁止这种方法了。
				.append("Date:").append(BLANK).append(this.getDate()).append(CRLF)
				.append("Content-Type:").append(BLANK).append(contentType).append(CRLF) //文件的 Content-Type 可通过Java获取。
				.append("Content-Length:").append(BLANK).append(length).append(CRLF).append(CRLF)
				.toString()
				.getBytes(Charset.forName("UTF-8"));
	}
	
	//获取时间
	private String getDate() {
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
		format.setTimeZone(TimeZone.getTimeZone("GMT")); // 设置时区为GMT  
		return format.format(date);
	}
}


说明: 这里的图片路径是我的计算机上面的路径,如果需要测试的话,需要改成自己本地的文件路径。

Test类

package com.dragon;

public class Test {
	public static void main(String[] args) {
		HttpServer httpServer = new HttpServer();
		httpServer.start();
	}
}

说明

这个demo是很简单的,我也根据这个写过复杂一点的,但是还是遇到一些问题,有的地方处理的不太好,主要还是我对这个Http协议只是简单的了解了,但是更多的细节就不太清楚了。这个例子作为学习计算机网络的传输层(TCP)和应用层(HTTP)的关系,是挺好的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值