weixin_44330842
年少无谓
2020-02-18 12:24
采纳率: 0%
浏览 503

自己搭建的tomcat服务器的url怎么写

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

public class HttpServer {

private static final Integer port = 80;//HTTP默认端口80

public static void main(String[] args) {

ServerSocket serverSocket; 

try {   

  //建立服务器Socket,监听客户端请求 

  serverSocket = new ServerSocket(port); 

  System.out.println("服务器端口:"+serverSocket.getLocalPort()); 

  //死循环不间断监听客户端请求 

  while(true){ 

    final Socket socket = serverSocket.accept(); 

    System.out.println("接收到一个请求,请求地址:"+  

        socket.getInetAddress()+":"+socket.getPort()); 

    //并发处理HTTP客户端请求 

    service(socket); 

  } 

} catch (IOException e) { 

  e.printStackTrace(); 

} 

}

public static void service(final Socket socket)

{

new Thread(){ 

  public void run(){ 

    InputStream inSocket; 

    try { 

      //获取HTTP请求头 

      inSocket = socket.getInputStream(); 

      int size = inSocket.available(); 

      byte[] buffer = new byte[size]; 

      inSocket.read(buffer); 

      String request = new String(buffer); 

      System.out.println("ClientBrowser:\n"+request+"\n"

          + "------------------------------------------------------------------"); 



      String firstLineOfRequest = ""; //http报文头

      String[] heads; //报文头的数据集合

      String uri = "/index.html"; 

      String contentType =""; 

      if(request.length() > 0){ 

        firstLineOfRequest = request.substring(0,request.indexOf("\r\n")); 

        heads = firstLineOfRequest.split(" "); 

        uri = heads[1]; 



        if(uri.indexOf("html") != -1){ 

          contentType = "text/html"; 

        }else{ 

          contentType = "application/octet-stream"; 

        } 

      } 

      System.out.println("http报文内容:"+request.substring(request.indexOf("\r\n"),request.length()));



      //将响应头发送给客户端 

      String responseFirstLine = "HTTP/1.1 200 OK\r\n"; 



      String responseHead = "Content-Type:" + contentType +"\r\n"; 



      OutputStream outSocket = socket.getOutputStream(); 

      System.out.println("ServerResponse:\n"+responseFirstLine+"\n"+responseHead+"\n"

          + "--------------------------------------------------------------------"); 

      outSocket.write(responseFirstLine.getBytes()); 

      outSocket.write(responseHead.getBytes()); 

      //通过HTTP请求中的uri读取相应文件发送给客户端 

      FileInputStream writehtml = new FileInputStream(new File("wwwroot"+uri)); 

      outSocket.write("\r\n".getBytes());  

      byte[] htmlbuffer = new byte[writehtml.available()]; 

      if(writehtml !=null){ 

        int len = 0; 

        System.out.println("writeHtml"); 

        while((len = writehtml.read(htmlbuffer)) != -1){ 

          outSocket.write(htmlbuffer, 0,len); 

        } 

      } 

      outSocket.close(); 

      socket.close(); 



    } catch (IOException e) { 

      e.printStackTrace(); 

    } 

  } 

}.start(); 

}

}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • caozhy

    private static final Integer port = 80;//HTTP默认端口80
    你用的默认80端口,那么可以用

    http://127.0.0.1 试试看

    点赞 评论
  • sanshizhang
    sanshizhang 2020-02-18 15:17

    这得看你部署在哪、如何运行了?如果只是在IDE里作为一个main类启动,那就是你的机器ip加上80端口。

    点赞 评论

相关推荐