• 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();
            }
            
        }
        
    }
  • 相关阅读:
    初识MVC web框架--Controller与View交互1
    Web框架UI系列--MVC常用控件讲解
    管理大师__Vuex
    语言国际化----vue-i18n
    uniapp动态获取高度
    vue__双向数据绑定v-model
    vue__模板解析3一般指令解析
    vue__模板解析2:事件指令解析
    vue__模板解析:大花括号表达式解析
    vue__数据代理
  • 原文地址:https://www.cnblogs.com/anholt/p/3664374.html
Copyright © 2020-2023  润新知