multithreaded static Web server development Mini-Test (Java)

Source: Internet
Author: User
Tags getmessage

multithreaded static Web server (original) Java version

buffer-based output for overcoming memory overflow problems

File One: Httpwebserveroflcl.java

Package COM.CN.LCL;

import Java.io.BufferedReader;

import Java.io.DataInputStream;

import java.io.File;

import Java.io.FileInputStream;

import java.io.IOException;

import Java.io.InputStreamReader;

import Java.io.PrintStream;

import Java.net.ServerSocket;

import Java.net.Socket;

import Java.net.URLDecoder;

Public class HTTPWEBSERVEROFLCL {

Private Static int counter = 0;

Public Static void Main (string[] args) {

WebServer ();

}

Public Static void WebServer () {

int PORT = 8080;

ServerSocket Server = null;

Socket client = null;

Try {

Server = new serversocket (PORT);

System. out. println ("Web server start, Access port number:" + port);

while (true) {

Client = Server.accept ();

counter+ +;

System. out. println ("First" + counter + "customer request");

New Communicationthread (client). Start ();

}

} catch (Exception e) {

E.printstacktrace ();

} finally {

Try {

Client.close ();

} Catch (IOException E1) {

E1.printstacktrace ();

}

Try {

Server.close ();

} catch (IOException e) {

E.printstacktrace ();

}

}

}

}

class Communicationthread extends Thread {

Socket client = null;

Public Communicationthread (Socket client) {

this. Client = client;

}

Public void Run () {

Try {

String Destip = client.getinetaddress (). toString ();

String Destport = client.getport () + "";

System. out. println ("Request Source IP:" + destip + "----Request Response Port:" + destport);

BufferedReader reader = new bufferedreader (new InputStreamReader (

Client.getinputstream ()));

String inputline = Reader.readline ();

System. out. println ("Inputline for:" + inputline);

String ResourceUrl = inputline.substring (

Inputline.indexof ('/') + 1, inputline.lastindexof ('/')-5);

int i = 0;

while ((Inputline = Reader.readline ()) = null) {

if (Inputline.equals ("")) {

break;

}

i++;

System. out. println ("First" + i + "line HTTP header information is:" + inputline);

}

ResourceUrl = Urldecoder. Decode (ResourceUrl, "UTF-8");

System. out. println ("Request resource:" + ResourceUrl);

File File = new file (ResourceUrl);

PrintStream OutStream = new printstream (Client.getoutputstream ());

if (File.exists () &&!file.isdirectory ()) {

System. out. println ("file exists");

Outstream.println ("http/1.0-ok");

Outstream.println ("mime_version:1.0");

Outstream.println ("content_type:text/html");

Outstream.println ("content_length:" + file.length ());

Outstream.println ("");

Transportfile (outstream, file);

Outstream.flush ();

} Else {

String notFound = "

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 ();

}

Outstream.close ();

long b = 1;

while (b < 3600000) {

b++;

}

Reader.close ();

Client.close ();

} catch (Exception e) {

SYSTEM.OUT.PRINTLN ("Error in the Run method:" + e.getmessage ());

} finally {

/*

* try {//client.close (); SYSTEM.OUT.PRINTLN ("This request was successfully closed! "); } Catch

* (IOException e) {e.printstacktrace ();}

*/

}

}

Public void transportfile (printstream ps, file file) {

Try {

DataInputStream in = new datainputstream (new fileinputstream (file));

byte temp[] = new byte[10240];

int a = 0;

while ((A = in.read (temp, 0, 10240))! =-1) {

Ps.write (temp);

Ps.flush ();

}

In.close ();

} catch (IOException e) {

System. out. println ("Error in file transfer, error message:" + e.getmessage ());

} finally {

Ps.close ();

System. out. println ("transmit complete");

}

}

}

file two: index.html

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >

<meta http-equiv="Content-type" content= "text/html; Charset=utf-8 ">

<title> my Java Web server </title>

<body>

<center>

Hello, daxiongjie!. This is wuyou!

Wish 2017

<form action="/index.html">

<img alt= "" src="test.jpg">

<br/>

<br/>

<br/>

<video width= "height=" controls= "controls">

<source src=" half Demon Allure mp4" type="Video/mp4" >

Your browser does not support this type of video format.

</video>

</form>

</center>

</body>

multithreaded static Web server development Mini-Test (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.