命令行编译java web项目_java命令行编译webserver

编译命令行:"C:\Program Files (x86)\Java\jdk1.8.0_20\bin\javac.exe"  *.java

运行命令:java WebServer

源码:

import java.io.*;

import java.net.*;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.util.*;

public class WebServer {

public static void main(String[] args) throws IOException {

try {

//connection number

int i = 1;

//establish server socket

ServerSocket s = new ServerSocket(8189);

while (true) {

//wait for client connection

Socket incoming = s.accept();

System.out.println("WebServer running on port: " + s.getLocalPort());

System.out.println("The connection number is: " + i);

Runnable r = new ThreadHandler(incoming);

Thread t = new Thread(r);

t.start();

i++;

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* This class handles the client input for one server socket

*/

class ThreadHandler implements Runnable {

private Socket incomig;

public ThreadHandler(Socket i) {

incomig = i;

}

public void run() {

try {

try {

InputStream inStream = incomig.getInputStream();

OutputStream outStream = incomig.getOutputStream();

Scanner in = new Scanner(inStream);

PrintWriter out = new PrintWriter(outStream, true);

while (true) {

String line = in.nextLine();

if (line.equals("\r\n") && line.equals("")) {

break;

}

System.out.println("the client request is :" + line);

//read request line

String[] requests = line.split("\\s+");

//read request head

Map headMap = null;

//read request body

Map parameterMap = null;

if (requests[0].equals("GET")) {

//response GET request

//respense with the file that the request wants

String path = System.getProperties().getProperty("user.dir") + requests[1].replaceAll("\\/", "//");

System.out.println("The file that the client wants is:" + path);

//read request head

headMap = readhead(in);

//read request body

parameterMap = readbody(in);

doGet(path, out);

} else if (requests[0].equals("POST")) {

//response POST request

//response with the file that the request wants and write the parameter at the of the file

String path = System.getProperties().getProperty("user.dir") + requests[1].replaceAll("\\/", "//");

System.out.println("The file that the client wants is:" + path);

//read request head

headMap = readhead(in);

//read request body

parameterMap = readbody(in);

doPost(path, out, parameterMap);

}

}

} finally {

incomig.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

private Map readhead(Scanner in) {

String headline = in.nextLine();

Map headMap = new HashMap<>();

while (!headline.equals("\r\n") && !headline.equals("")) {

String[] keyValue = {"", ""};

String[] temp = headline.split(": |:");

if (temp.length == 2) {

keyValue = temp;

}

if (temp.length == 1) {

keyValue[0] = temp[0];

}

headMap.put(keyValue[0], keyValue[1]);

headline = in.nextLine();

}

return headMap;

}

private Map readbody(Scanner in) {

String bodyline = in.nextLine();

if (bodyline.equals("\r\n") || bodyline.equals("")) {

return null;

}

String t = in.nextLine();

while (!t.equals("\r\n") && !t.equals("")) {

bodyline += t + "\r\n";

t = in.nextLine();

}

String[] parameters = bodyline.split("&");

Map parameterMap = new HashMap<>();

String[] keyV = {"", ""};

for (String parameter : parameters) {

String[] temp = parameter.split("=");

if (temp.length == 2) {

keyV = temp;

}

if (temp.length == 1) {

keyV[0] = temp[0];

}

parameterMap.put(keyV[0], keyV[1]);

}

return parameterMap;

}

private void doGet(String requestPath, PrintWriter out) {

final String CRLF = "\r\n";

String statusLine = null;

String date = new Date().toString();

String contentTypeLine = "";

String contentLengthLine = "";

String entityBody = "";

File requestFile = new File(requestPath);

Path path = Paths.get(requestPath);

if (requestFile.exists()) {

statusLine = "HTTP/1.0 200 OK";

contentLengthLine = String.valueOf(requestFile.length());

try {

contentTypeLine = Files.probeContentType(path);

List contents = Files.readAllLines(path);

for (String line : contents) {

entityBody += line + CRLF;

}

} catch (IOException e) {

statusLine = "HTTP/1.0 400 BadRequest";

entityBody = "400 Not BadRequest" +

"

400 BadRequest" +

e.getMessage();

e.printStackTrace();

}

} else {

statusLine = "HTTP/1.0 404 Not Found";

entityBody = "404 Not Found" +

"

404 Not Found";

}

out.print(statusLine + CRLF);

out.print("Date: " + date + CRLF);

out.print("Content-Type: " + contentTypeLine + CRLF);

out.print("Content-Length: " + contentLengthLine + CRLF);

out.print(CRLF);

out.print(entityBody + CRLF);

out.flush();

}

private void doPost(String requestPath, PrintWriter out, Map parameterMap) {

final String CRLF = "\r\n";

String statusLine = null;

String date = new Date().toString();

String contentTypeLine = "";

String contentLengthLine = "";

String entityBody = "";

File requestFile = new File(requestPath);

Path path = Paths.get(requestPath);

if (requestFile.exists()) {

statusLine = "HTTP/1.0 200 OK";

try {

PrintWriter fileWrite = new PrintWriter(new FileWriter(requestFile, true));

fileWrite.append(parameterMap.toString());

fileWrite.close();

contentLengthLine = String.valueOf(requestFile.length());

contentTypeLine = Files.probeContentType(path);

List contents = Files.readAllLines(path);

for (String line : contents) {

entityBody += line + CRLF;

}

} catch (IOException e) {

statusLine = "HTTP/1.0 400 BadRequest";

entityBody = "400 Not BadRequest" +

"

400 BadRequest" +

e.getMessage();

e.printStackTrace();

}

} else {

statusLine = "HTTP/1.0 404 Not Found";

entityBody = "404 Not Found" +

"

404 Not Found";

}

out.print(statusLine + CRLF);

out.print("Date: " + date + CRLF);

out.print("Content-Type: " + contentTypeLine + CRLF);

out.print("Content-Length: " + contentLengthLine + CRLF);

out.print(CRLF);

out.print(entityBody + CRLF);

out.flush();

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值