java实现http协议_JAVA实现简单的 web容器,HTTP协议 | 学步园

WebServer.java

/*

* $RCSfile: WebServer.java,v $

* $Revision: 1.1 $

* $Date: 2009-2-20 $

*

* Copyright (C) 2005 Bettem, Inc. All rights reserved.

*

* This software is the proprietary information of Bettem, Inc.

* Use is subject to license terms.

*/

package com.lihan;

/**

*

Title: WebServer

*

Description:

*

Copyright: Copyright (c) 2006

* @author 李晗

* @version 1.0

*/

import java.net.*;

public class WebServer {

public static void main(String args[]) {

int i = 1, PORT = 807;

ServerSocket server = null;

Socket client = null;

try {

server = new ServerSocket(PORT);

System.out.println("Web Server is listening on port "+ server.getLocalPort());

for (;;)

{

client = server.accept(); // 接受客户机的连接请求

new ConnectionThread(client, i).start();

i++;

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

ConnectionThread.java

/*

* $RCSfile: ConnectionThread.java,v $

* $Revision: 1.1 $

* $Date: 2009-2-20 $

*

* Copyright (C) 2005 Bettem, Inc. All rights reserved.

*

* This software is the proprietary information of Bettem, Inc.

* Use is subject to license terms.

*/

package com.lihan;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.PrintStream;

import java.net.Socket;

/**

*

* Title: ConnectionThread

*

*

* Description:

*

*

* Copyright: Copyright (c) 2006

*

*

* @author 李晗

* @version 1.0

*/

class ConnectionThread extends Thread {

Socket client; // 连接Web浏览器的socket字

int counter; // 计数器

public ConnectionThread(Socket cl, int c) {

client = cl;

counter = c;

}

public void run() // 线程体

{

try {

PrintStream outstream = new PrintStream(client.getOutputStream());

System.out.println("aaa"+client.getOutputStream());

DataInputStream instream = new DataInputStream(client.getInputStream());

String inline = instream.readLine(); // 读取Web浏览器提交的请求信息,主要是这里,获取协议

System.out.println("Received:" + inline);

if (getrequest(inline)) { // 如果是GET请求

String filename = getFilename(inline);

File file = new File(filename);

System.out.println("File Path:"+file.getAbsolutePath());

if (file.exists())

{ // 若文件存在,则将文件送给Web浏览器

outstream.println("HTTP/1.0 200 OK");

outstream.println("MIME_version:1.0");

outstream.println("Content_Type:text/html");

int len = (int) file.length();

outstream.println("Content_Length:" + len);

outstream.println("");

sendFile(outstream, file); // 发送文件

outstream.flush();

}

else

{ // 文件不存在时

String notfound = "

Not Found

Error 404-file not found

";

outstream.println("HTTP/1.0 404 no found");

outstream.println("Content_Type:text/html");

outstream.println("Content_Length:" + notfound.length() + 2);

outstream.println("");

outstream.println(notfound);

outstream.flush();

}

}

long m1 = 1;

while (m1 < 11100000)

{

m1++;

} // 延时

client.close();

} catch (IOException e) {

e.printStackTrace();

}

}

/* 获取请求类型是否为“GET” */

boolean getrequest(String s) {

if (s.length() > 0)

{

if (s.substring(0, 3).equalsIgnoreCase("GET"))

{

return true;

}

}

return false;

}

/* 获取要访问的文件名 */

String getFilename(String s)

{

String f = s.substring(s.indexOf(' ') + 1);

f = f.substring(0, f.indexOf(' '));

try

{

if (f.charAt(0) == '/')

{

f = f.substring(1);

}

}

catch (StringIndexOutOfBoundsException e) {

System.out.println("Exception:" + e);

}

if (f.equals(""))

{

f = "index.html";//设置默认首页

}

return f;

}

/* 把指定文件发送给Web浏览器 */

void sendFile(PrintStream outs, File file) {

try {

DataInputStream in = new DataInputStream(new FileInputStream(file));

int len = (int) file.length();

byte buf[] = new byte[len];

in.readFully(buf);

outs.write(buf, 0, len);

outs.flush();

in.close();

}

catch (Exception e)

{

e.printStackTrace();

System.exit(1);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值