Java HTTP protocol Server Demo

Source: Internet
Author: User
Tags ranges

Tag:java    Server    http protocol    

Import java.io.*;  Import java.net.*; /** * Myhttpserver implement a simple HTTP server side, you can get the user submitted content * and give the user a response * because of the time of the relationship, the HTTP header processing is not standardized * **/public class Myhttpser      ver {//server root directory, post.html, upload.html are placed in the location public static String Web_root = "C:/root";      Port private int port;      The URL of the file requested by the user private String Requestpath;      Mltipart/form-data method submits the delimiter for post, private String boundary = null;        The length of the body of the post submission request is private int contentlength = 0;          Public Myhttpserver (String root, int port) {web_root = root;          This.port = port;      Requestpath = null; }//Handle GET request private void doget (DataInputStream reader, outputstream out) throws Exception {if (new Fil E (Web_root + This.requestpath). Exists ()) {//Find the requested file from the server root and send it back to the browser inputstream Filein = new F              Ileinputstream (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."); }}//processing POST request private void DoPost (DataInputStream reader, outputstream out) throws Exception {St          Ring line = Reader.readline ();              while (line! = null) {System.out.println (line);              line = Reader.readline ();              if ("". Equals (line)} {break; } else if (Line.indexof ("Content-length")! =-1) {this.contentlength = Integer.parseint (line.substring (l              Ine.indexof ("content-length") + 16);              }//indicates to upload an attachment and jumps to the Domultipart method. else if (Line.indexof ("Multipart/form-data")! =-1) {//Multiltipart delimiter this.boundary = l                  Ine.substring (Line.indexof ("boundary") + 9);                  This.domultipart (reader, out);              Return }          }          Continue to read System.out.println ("Begin reading posted data ...") submitted by normal post (no attachments);          String dataline = null;          The user sends the Post data body 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));          }//Send back content to the browser String response = "";          Response + = "http/1.1 ok/n";          Response + = "Server:sunpache 1.0/n";          Response + = "content-type:text/html/n";          Response + = "Last-modified:mon, 1998 13:23:42 gmt/n";          Response + = "Accept-ranges:bytes";          Response + = "/n"; String BODY = "

Java HTTP protocol Server Demo

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.