java怎么java实现http服务器器怎么解决

本帖子已过去太久远了,不再提供回复功能。Java实现简单的HTTP服务器 - 开源中国社区
当前访客身份:游客 [
当前位置:
发布于 日 14时,
Java实现简单的HTTP服务器
代码片段(2)
1.&[代码]Java实现简单的HTTP服务器&&&&
import java.net.*;
import java.nio.ByteB
import java.nio.channels.*;
import java.nio.charset.*;
import java.util.concurrent.*;
import java.io.*;
public class SimpleHttpServer {
private int port = 80;
private ServerSocketChannel serverSocketChannel =
private ExecutorService executorS
private static final int POOL_MULTIPLE = 4;
public SimpleHttpServer() throws IOException {
executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors() * POOL_MULTIPLE);
serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().setReuseAddress(true);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
public void service() {
while (true) {
SocketChannel socketChannel =
socketChannel = serverSocketChannel.accept();
executorService.execute(new Handler(socketChannel));
} catch (IOException e) {
e.printStackTrace();
public static void main(String[] args) throws IOException {
new SimpleHttpServer().service();
class Handler implements Runnable {
private SocketChannel socketC
public Handler(SocketChannel socketChannel) {
this.socketChannel = socketC
public void run() {
handle(socketChannel);
private void handle(SocketChannel socketChannel) {
Socket socket = socketChannel.socket();
System.out
.println(socket.getInetAddress() + ":" + socket.getPort());
ByteBuffer buffer = ByteBuffer.allocate(1024);
socketChannel.read(buffer);
buffer.flip();
String request = decode(buffer);
StringBuffer sb = new StringBuffer("HTTP/1.1 200 OK\r\n");
sb.append("Content-Type:text/html\r\n\r\n");
socketChannel.write(encode(sb.toString()));
FileInputStream in =
String firstLineOfRequest = request.substring(0,
request.indexOf("\r\n"));
if (firstLineOfRequest.indexOf("login.htm") != -1)
in = new FileInputStream("login.htm");
in = new FileInputStream("hello.htm");
FileChannel fileChannel = in.getChannel();
fileChannel.transferTo(0, fileChannel.size(), socketChannel);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socketChannel != null)
socketChannel.close();
} catch (IOException e) {
e.printStackTrace();
private Charset charset = Charset.forName("GBK");
private ByteBuffer encode(String string) {
return ByteBuffer.allocate(string.length() * 2).get(
string.getBytes(charset));
private String decode(ByteBuffer buffer) {
byte[] source = new byte[buffer.position() + 1];
buffer.put(source);
return new String(source, charset);
2.&[代码]login.htm&&&&
&title&helloapp&/title&
&form name="loginForm" method="post" action="hello.htm"&
&tr&&td&&div align="right"&用户名:&/div&&/td&
&td&&input type="text" name="username"&&/td&
&tr&&td&&div align="right"&密码:&/div&&/td&
&td&&input type="password" name="password"&&/td&
&tr&&td&&/td&
&td&&input type="submit" name="submit" value="submit"&&/td&
开源中国-程序员在线工具:
64 行substring数组下标越界吧?&
String firstLineOfRequest = request.substring(0,request.indexOf(&\r\n&)+1);
我用chrome测试都是下载文件
2楼:曹方毅 发表于
3楼:长安俞白眉 发表于
干的漂亮!
4楼:浪客Dandy 发表于
Java实现HTTP 应该用Apache HTTP Core或者Sun HTTP Server
5楼:牛牛牛牛 发表于
引用来自“浪客Dandy”的评论Java实现HTTP 应该用Apache HTTP Core或者Sun HTTP Server上面是通过socket实现
6楼:李克华 发表于
7楼:chenliecan 发表于
注释 都没有。。。。
8楼:牟宗民 发表于
9楼:宋鑫001 发表于
10楼:宋鑫001 发表于
好,过了一个月不经意看到居然看明白了,NIO实现的HTTP服务器,我用BIO实现了一个,不知道2个性能差多少
开源从代码分享开始
DTC2的其它代码&&&&&&&&&&&&&
原文链接:
首先了解下HTTP协议:
wikiPedia的说明很好,在此不重复了。链接:
源码分析:
概述:此两个代码段,完成服务器监听,线程处理短请求服务和应答(符合伪HTTP协议,&伪&即判断协议格式不严谨)。展示了利用java socket完成http通信的核心原理代码,希望给初次学习socket或者第一次利用java来实现HTTP协议服务器的朋友提供些帮助。
利用 Java Socket 网络编程来绑定服务器某个端口进行监听。
1 package JHS
3 import java.io.IOE
4 import java.net.ServerS
5 import java.net.S
7 public class Server {
private static int PORT = 1720;
public static String charset = "utf-8";
private ServerS
public static void main(String[] args) {
new Server();
public Server() {
this.ss = new ServerSocket(this.PORT);
21 System.out.println("Server Listening Port " + this.PORT + "...");
boolean isGo = true;
while(isGo){
this.client = this.ss.accept();
25 System.out.println("One Client Connected. " + this.client);
ClientThread ct = new ClientThread(this.client);
Thread t = new Thread(ct);
t.start();
29 System.out.println("One Client Thread Already Started...");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(this.ss != null){
this.ss.close();
this.ss = null;
if(this.client != null){
this.client.close();
this.client = null;
} catch(IOException e) {
e.printStackTrace();
说明:上述代码完成了服务端口(1720,你懂么)的监听,并同时利用线程了处理每个客户端(Client )的消息请求,以减少服务器处理&短请求&阻塞问题。
下述代码完成请求是否符合伪HTTP协议,并完成响应消息。
1 package JHS
3 import java.io.*;
4 import java.net.S
5 import java.util.D
7 import action.ManagerEachMatchI
9 import view.*;
11 public class ClientThread implements Runnable {
private float requestDelay = (float)0.5;//
private Socket _s;
private BufferedR
private PrintS
public ClientThread(Socket s) {
public void run() {
this.i = new BufferedReader(new InputStreamReader(this._s.getInputStream()));
this.o = new PrintStream(this._s.getOutputStream());
String request = this.getValidRequest();
if( !request.equals("") ) {
System.out.print("当前虚拟机最大可用内存为:");
System.out.println(Runtime.getRuntime().maxMemory()/+"M");
System.out.print("当前,虚拟机已占用内存:");
System.out.println(Runtime.getRuntime().totalMemory()/+"M");
this.o.println("HTTP/1.1 200 OK");
Date now = new Date();
this.o.println("Data:" + now);
this.o.println("Server: JHServer");
this.o.println("Access-Control-Allow-Origin:*");
this.o.println("Content-Type: text/ charset=UTF-8");
this.o.println();
String content = null;
IndeMatchInfo imi = new IndeMatchInfo(22,22,2,2,22,(double)0.0,"S7-200");
content = imi.getHTML();
imi.clear();
imi = null;
System.out.print("当前虚拟机最大可用内存为:");
System.out.println(Runtime.getRuntime().maxMemory()/+"M");
System.out.print("当前,虚拟机已占用内存:");
System.out.println(Runtime.getRuntime().totalMemory()/+"M");
this.o.println(content);
content = "";
this.o.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
this.close();
private String getValidRequest() {
float second = (float) 0.0;
boolean isGo = true;
String request = null;
while (!this.i.ready()) {
second += 0.01;
if (second & this.requestDelay) {
System.out.println("One Client Delayed " + this._s);
isGo = false;
Thread.sleep(10);
if (isGo == true) {
request = this.i.readLine();
if( request.contains("GET /") && request.contains(" HTTP/") ) {
request = request.substring(request.indexOf("/"),request.indexOf(" HTTP/"));
88 System.out.println("Client Request Info: " + request);
isGo = false;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(isGo == true) {
return "null";
private void close() {
if (this.i != null) {
this.i.close();
this.i = null;
if (this.o != null) {
this.o.close();
this.o = null;
if (this._s != null) {
this._s.close();
this._s = null;
120 System.out.println("One Client Disconnected...");
} catch (IOException e) {
e.printStackTrace();
1、getValidRequest() 验证请求消息是否满足伪HTTP协议。比如:GET /images/logo.gif HTTP/1.1,则会返回"/images/logo.gif"。若不符合伪HTTP协议或者请求时不发送任何消息头(比如通过telnet访问,此情况最大延迟0.5秒),则会返回空串。
2、close(),释放内存,同时线程结束。
3、run(),短请求的线程主体,在此处可以进行数据逻辑处理,并返回特定消息内容。
&posted on
阅读(...) 评论()54154人阅读
Java基础(30)
用java socket实现了一个简单的http服务器, 可以处理GET, POST,以及带一个附件的multipart类型的POST。虽然中途遇到了很多问题, 不过通过在论坛和几个高手交流了一下,问题都解决了。如果你觉得程序有些地方看不明白,可以参看这个帖子:.
虽然解析http头不是很规范,本来应该用原始的字节流, 我采用了一个折衷的方案,用DataInputStream.
本代码的实用性==0,但是可以帮助很好地了解http协议,然后其他的应用层协议大都如此。
如果你从来都没有了解过http协议,建议先搜索阅读一下,或者你还可以用下面的代码来简单的看一看到底浏览器和服务器之间都相互发送了什么数据。
MyHttpClient.java: 模拟浏览器的行为, 向服务器发送get/post请求,然后打印出服务器返回的消息。这样就可以查看当一个请求到来之后, 服务器到底都给浏览器发送了哪些消息。
import java.io.*;
import java.net.*;
public class MyHttpClient {
public static void main(String[] args) throws Exception{
InetAddress inet = InetAddress.getByName("");
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-q=0.5");
writer.println("Accept-Encoding: gzip, deflate");
writer.println("Host: ");
writer.println("User-Agent: Mozilla/4.0 ( 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.; .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端接收浏览器的请求,然后把整个请求的报文打印出来。程序运行之后直接用浏览器测试。
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[];
InputStream in = socket.getInputStream();
int byteRead = in.read(buf, 0, );
String dataString = new String(buf, 0, byteRead);
System.out.println(dataString);
in.close();
socket.close();
主程序MyHttpServer.
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";
//用户请求的文件的url
private String requestP
//mltipart/form-data方式提交post的分隔符,
private String boundary =
//post提交请求的正文的长度
private int contentLength = 0;
public MyHttpServer(String root, int port) {
WEB_ROOT =
this.port =
requestPath =
//处理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();
if ("".equals(line)) {
} 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);
//继续读取普通post(没有附件)提交的数据
System.out.println("begin reading posted data......");
String dataLine =
//用户发送的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 :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)) {
} 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
&这里有一个空行,表明接下来的内容都是要提交的正文&
-----------------------------7d&这是multipart分隔符&
Content-Disposition: form- name="myfile"; filename="mywork.doc"
Content-Type: text/plain
&附件正文&........................................
.................................................
-----------------------------7d&这是multipart分隔符&
Content-Disposition: form- name="myname"&其他字段或附件&
&这里有一个空行&
&其他字段或附件的内容&
-----------------------------7d--&这是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 +=
//用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().
pos = dataString.indexOf(boundary, pos) - 4;
//附件结束位置
int end = dataString.substring(0, pos).getBytes().
//以下找出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 :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请求
&title&test my server&/title&
&meta http-equiv="Content-Type" content="text/ charset=utf-8"&
&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"&
upload.html:测试带附件的post请求
&title&my page&/title&
border-collapse:
来自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' /&
一切准备妥当,并且MyHttpServer运行之后, 在浏览器输入http://localhost:8080/post.html和即可进行测试.
转载请注明来自
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:765911次
积分:9332
积分:9332
排名:第1657名
原创:173篇
转载:34篇
评论:437条
(1)(2)(1)(15)(2)(1)(2)(3)(1)(3)(13)(1)(2)(7)(5)(2)(6)(1)(8)(20)(1)(3)(1)(4)(4)(19)(28)(13)(8)(9)(14)(4)(5)(1)}

我要回帖

更多关于 java实现https服务器 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信