<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 40, 文章 - 0, 評論 - 20, 引用 - 0
    數(shù)據(jù)加載中……

    用Java實現(xiàn)的Html web服務(wù)器

    ? 馬上就要開始轉(zhuǎn)到新的項目組,做一個全新的項目了,對于HTTP協(xié)議需要一定的了解,所以周末自己用Java寫了一個簡單的web服務(wù)器試試,只能實現(xiàn)簡單的html文件瀏覽。

    主要包括三個類:WebServer(監(jiān)聽瀏覽器請求),SocketThread(處理瀏覽器請求的進(jìn)程),StringUtil(實現(xiàn)一些公共的操作),下面是三個類的代碼.


    ----WebServer----
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class WebServer {
    ?
    ?public static void main(String[] argv) throws IOException {
    ??ServerSocket servSocket = new ServerSocket(StringUtil.LISTENING_PORT);
    ??try {
    ???while (true) {
    ????Socket socket = servSocket.accept();
    ????new SocketThread(socket).start();
    ???}
    ??} finally {
    ???servSocket.close();
    ??}
    ?}
    }

    ---SocketThread------
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintStream;
    import java.io.Writer;
    import java.net.Socket;

    public class SocketThread extends Thread {
    ?private Socket socket = null;

    ?public SocketThread(Socket s) {
    ??this.socket = s;
    ?}

    ?public void run() {
    ??try {
    ???if (socket == null) {
    ????throw new Exception("==>SOCKET為空<==");
    ???}
    ???BufferedReader reader = new BufferedReader(new InputStreamReader(
    ?????socket.getInputStream()));
    ???String fileName = "";
    ???while (true) {
    ????String str = reader.readLine();
    ????if (str == null || str.length() <= 0) {
    ?????break;
    ????}
    ????//System.out.println("===>"+str);
    ????if (StringUtil.isGetRequestInfo(str)) {
    ?????fileName = StringUtil.getFileName(str);
    ?????break;
    ????}
    ???}
    ???//System.out.println("===>客戶機(jī)IP==>"+socket.getInetAddress().toString());
    ???//System.out.println("===>客戶機(jī)端口==>"+socket.getPort());???
    ???/*
    ??? BufferedWriter writer = new BufferedWriter(new
    ??? OutputStreamWriter(socket.getOutputStream()));
    ??? */
    ???PrintStream outputStream = new PrintStream(socket.getOutputStream());
    ???File file = new File(StringUtil.WEBPATH + fileName);
    ???if (file.exists()) { //如果文件存在
    ????StringUtil.sendHttpHead(outputStream, file);
    ????StringUtil.sendFile(outputStream, file);
    ????outputStream.flush();
    ???} else { //文件沒找到,返回404頁面
    ????StringUtil.send404Page(outputStream);
    ????outputStream.flush();
    ???}
    ??} catch (Exception e) {
    ???e.printStackTrace();
    ??} finally {
    ???try {
    ????socket.close();
    ???} catch (Exception e) {
    ???}
    ??}
    ?}

    }



    ---StringUtil-----
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.PrintStream;

    /**
    ?* @author xiaoliang
    ?*/
    public class StringUtil {

    ?// 服務(wù)器監(jiān)聽的端口
    ?public static final int LISTENING_PORT = 8080;

    ?// 服務(wù)器文件的位置
    ?public static final String WEBPATH = "E:";

    ?/**
    ? * 判斷該字符串是不是瀏覽器發(fā)送過來的請求頭信息
    ? * @param str
    ? * @return
    ? */
    ?public static boolean isGetRequestInfo(String str) {
    ??if (str == null || str.length() <= 0)
    ???return false;
    ??boolean isGetStr = true;
    ??if (str.indexOf("GET") != 0) {
    ???isGetStr = false;
    ??}
    ??if (str.indexOf("HTTP/") <= 0) {
    ???isGetStr = false;
    ??}
    ??return isGetStr;
    ?}

    ?/**
    ? * 獲得請求信息中的文件名,默認(rèn)為index.html
    ? *
    ? * @param str
    ? * @return
    ? */
    ?public static String getFileName(String str) {
    ??String fileName = "index.html", s;
    ??int httpIndex = str.lastIndexOf("HTTP/");
    ??s = str.substring(3, httpIndex);
    ??s = s.trim();
    ??if (s != null && s.length() > 0 && s.indexOf(".") > 0) {
    ???fileName = s;
    ??}
    ??return fileName;
    ?}

    ?/**
    ? * 發(fā)送文件到客戶端
    ? *
    ? * @param out
    ? * @param file
    ? */
    ?public static void sendFile(PrintStream out, File file) {
    ??try {
    ???DataInputStream in = new DataInputStream(new FileInputStream(file));
    ???int length = (int) file.length();
    ???byte[] buffer = new byte[length];
    ???in.readFully(buffer);
    ???out.write(buffer, 0, length);
    ???in.close();
    ??} catch (Exception e) {
    ???e.printStackTrace();
    ??}
    ?}

    ?/**
    ? * 發(fā)送返回的頭部信息
    ? * @param out
    ? */
    ?public static void sendHttpHead(PrintStream outputStream, File file) {
    ??try {
    ???outputStream.println("HTTP/1.0200OK");
    ???outputStream.println("Content_Type:text/htm1");
    ???outputStream.println("Content_Length:" + file.length());
    ???outputStream.println("Server:moon webserver 1.0");
    ???outputStream.println("");
    ??} catch (Exception e) {
    ???e.printStackTrace();
    ??}
    ?}

    ?/**
    ? * 返回404頁面
    ? * @param out
    ? */
    ?public static void send404Page(PrintStream out) {
    ??try {
    ???out.println("HTTP /1.0 404 no found");
    ???out.println("Content_type:text/html");
    ???out.println("");
    ???out.println("Error404:file not found!");
    ??} catch (Exception e) {
    ???e.printStackTrace();
    ??}
    ?}
    ?
    ?public static void main(String[] argv) {
    ??String str = "GET /11.html HTTP/1.1";
    ??str = StringUtil.getFileName(str);
    ??System.out.println("==>" + str + "<==");
    ??File file = new File(StringUtil.WEBPATH + str);
    ??if (file.exists()) {
    ???System.out.println("exists");
    ??} else {
    ???System.out.println("not exists");
    ??}
    ?}

    }

    posted on 2006-03-28 13:55 月亮 閱讀(457) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲成在人线aⅴ免费毛片| 蜜芽亚洲av无码精品色午夜| 亚洲天堂男人影院| 免费福利视频导航| 色婷婷亚洲十月十月色天| 91大神免费观看| 亚洲国产夜色在线观看| 免费看污成人午夜网站| 亚洲国产综合精品中文第一| 毛片免费vip会员在线看| 在线aⅴ亚洲中文字幕| 无码人妻精品一二三区免费| 亚洲熟女精品中文字幕| 国产免费拔擦拔擦8x| 牛牛在线精品免费视频观看| 久久亚洲国产精品五月天婷| 99热在线日韩精品免费| 亚洲综合日韩中文字幕v在线| 1000部啪啪毛片免费看| 亚洲中文无码永久免| 国产91在线免费| 十八禁视频在线观看免费无码无遮挡骂过| 国产亚洲精AA在线观看SEE| 在线成人爽a毛片免费软件| 亚洲专区一路线二| 国产精品久久久久影院免费| 一区二区三区在线免费观看视频| 亚洲色婷婷六月亚洲婷婷6月| 最近2019中文字幕免费大全5| 亚洲精品第一综合99久久| 亚洲精品成a人在线观看| 亚欧免费无码aⅴ在线观看| 亚洲不卡1卡2卡三卡2021麻豆| 蜜臀91精品国产免费观看| 中文在线观看国语高清免费| 777亚洲精品乱码久久久久久 | 久久精品国产亚洲AV久| 免费v片视频在线观看视频| 久久国产精品免费视频| 亚洲乱色伦图片区小说| 亚洲精品中文字幕无码蜜桃|