用java写的服务器

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

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值