用java写的服务器
用java写了一个服务器,其中有Request请求类、ProcessFile处理类、Response响应类、HttpServer服务端、 还写了个main方法程序入口,代码分享如下:
HttpServer服务端
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class HttpServer {
ServerSocket serverSocket=null;
private static int port=8000;
public HttpServer(){
System.out.println("coming--HttpServer");
try {
serverSocket = new ServerSocket(port);
System.out.println("serverSocket--->"+serverSocket);
while(true){
System.out.println("loop");
Socket socket=serverSocket.accept();
System.out.println("socket--->"+socket);
Request request = new Request(socket,socket.getInputStream());
socket.close();
}
} catch (IOException e) {
System.out.println("error!"+e.getMessage());
}
}
}
Request请求类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
public class Request {
private static InputStream inputStream=null;
private static Socket socket=null;
public Request(Socket socket, InputStream getInputStream) throws IOException {
this.inputStream=getInputStream;
this.socket=socket;
System.out.println("coming---Request()");
getURL();
}
public void getURL() throws IOException {
System.out.println("coming------getURL");
String url=null;
try {
System.out.println("coming try 003");
BufferedReader in=new BufferedReader(new InputStreamReader(inputStream));
System.out.println("in-->"+in);
String line=in.readLine();
System.out.println("line------>"+line);
url=line.substring(line.indexOf("/")+1,line.lastIndexOf("/")-5);
System.out.println("url---->"+url);
} catch (IOException e) {
System.out.println("获取文件资源失败!"+e.getMessage());
}
if(url != null){
ProcessFile pf=new ProcessFile(url,socket);
}
}
}
ProcessFile处理类
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
public class ProcessFile {
private static String url=null;
private static Socket socket=null;
public ProcessFile(String url, Socket socket) throws IOException {
this.url=url;
this.socket=socket;
getOutContent();
}
public void getOutContent() throws IOException {
File file=new File(url);
System.out.println("file----->"+url);
FileInputStream fileStream=new FileInputStream(file.getAbsolutePath());
System.out.println("fileStream-->"+fileStream);
byte[] data=new byte[fileStream.available()];
System.out.println("data----------------->"+data.length);
fileStream.read(data);
String datas=new String(data);
if(datas != null){
Response response=new Response(datas,socket);
}
fileStream.close();
}
}
Response响应类
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
public class Response {
private static String datas=null;
private static Socket socket=null;
public Response(String datas, Socket socket) throws IOException {
System.out.println("coming ---- Response() 0005");
this.datas=datas;
this.socket=socket;
outHTML();
}
public void outHTML() throws IOException {
System.out.println("comging outHTML() 44455");
new PrintWriter(socket.getOutputStream(),true).println(datas);
}
}
main方法程序入口
public class TestHttpServer {
/**
* @param args
*/
public static void main(String[] args) {
new HttpServer();
}
}