zoukankan      html  css  js  c++  java
  • java socket 自写静态服务器 apache

    需要在F盘下创建ooo文件夹,html页面放置其中  就可以在浏览器中访问    此端口监听 8080

    package cn.com.test09;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class t14 {
    
        public static void main(String[] args) {
            try {
                ServerSocket ss= new ServerSocket(8080);
                while(true){
                    Socket soc = ss.accept();
                    new Thread(new ServerB(soc)).start();
                }
                
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    class ServerB implements Runnable{
        private Socket soc;
        private BufferedReader br;
        private PrintWriter dos;
        String html ;
        ServerB(Socket soc){
            this.soc=soc;
            try {
                br=new BufferedReader(new InputStreamReader(soc.getInputStream()));
                dos= new PrintWriter(soc.getOutputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        private String[] getHtml(String str,String split){
            return str.split(split);
            
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            String s;
            try {
                String one = br.readLine();
                if(!(one.equals("GET /favicon.ico HTTP/1.1"))){
                    
                     html =getHtml(getHtml(one," ")[1],"\?")[0];
                    
                     File f= new File("F:/ooo"+html);
                        InputStream in= new FileInputStream(f);
                        byte[] b= new byte[(int) f.length()];
                        in.read(b);
                        dos.write(new String(b));
                        dos.flush();
                        System.out.println(b);
    //                    OutputStream ssss = soc.getOutputStream();
                        System.out.println("===========");
    //                    ssss.write(b);
    //                    ssss.flush();
                        in.close();
                }
                
                
                br.close();
            
                dos.close();
                //ssss.close();
                soc.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
    }
  • 相关阅读:
    java中Annotation注解的定义与使用
    ABC184 D——F && 一道LC好题
    YZYのPython 作业~
    杂谈(11.13——lca && mst)
    树状数组(BIT)—— 一篇就够了
    Codeforces Round #673 (Div. 2)[A-E]
    Codeforces Round #674 (Div. 3)
    Educational Codeforces Round 95 (Rated for Div. 2) [A -- E]
    LEETCODE 第 205 场周赛
    Codeforces Round #662 (Div. 2)
  • 原文地址:https://www.cnblogs.com/anholt/p/3664374.html
Copyright © 2011-2022 走看看