在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ SocketHttpServer無法啟動,需要在maven中怎么配置

SocketHttpServer無法啟動,需要在maven中怎么配置


package com.socket;

import java.net.Socket;

public class SocketHttpServer implements Runnable {

    private final static int PORT = 28081;
    private ServerSocket server = null;

    public static void main(String[] args) {
        new SocketHttpServer();
    }

    public SocketHttpServer() {
        try {
            server = new ServerSocket(PORT);
            if (server == null)
                System.exit(1);
            new Thread(this).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                Socket client = null;
                client = server.accept();
                if (client != null) {
                    try {
                        System.out.println("連接服務(wù)器成功!!...");

                        BufferedReader reader = new BufferedReader(
                                new InputStreamReader(client.getInputStream()));

                        // GET /test.jpg /HTTP1.1
                        String line = reader.readLine();

                        System.out.println("line: " + line);

                        String resource = line.substring(line.indexOf('/'),
                                line.lastIndexOf('/') - 5);

                        System.out.println("the resource you request is: "
                                + resource);

                        resource = URLDecoder.decode(resource, "UTF-8");

                        String method = new StringTokenizer(line).nextElement()
                                .toString();

                        System.out.println("the request method you send is: "
                                + method);

                        while ((line = reader.readLine()) != null) {
                            if (line.equals("")) {
                                break;
                            }
                            System.out.println("the Http Header is : " + line);
                        }

                        if ("post".equals(method.toLowerCase())) {
                            System.out.println("the post request body is: "
                                    + reader.readLine());
                        }

                        if (resource.endsWith(".mkv")) {

                            transferFileHandle("videos/test.mkv", client);
                            closeSocket(client);
                            continue;

                        } else if (resource.endsWith(".jpg")) {

                            transferFileHandle("images/test.jpg", client);
                            closeSocket(client);
                            continue;

                        } else if (resource.endsWith(".rmvb")) {

                            transferFileHandle("videos/test.rmvb", client);
                            closeSocket(client);
                            continue;

                        } else {
                            PrintStream writer = new PrintStream(
                                    client.getOutputStream(), true);
                            writer.println("HTTP/1.0 404 Not found");// 返回應(yīng)答消息,并結(jié)束應(yīng)答
                            writer.println();// 根據(jù) HTTP 協(xié)議, 空行將結(jié)束頭信息
                            writer.close();
                            closeSocket(client);
                            continue;
                        }
                    } catch (Exception e) {
                        System.out.println("HTTP服務(wù)器錯(cuò)誤:"
                                + e.getLocalizedMessage());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void closeSocket(Socket socket) {
        try {
            socket.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println(socket + "離開了HTTP服務(wù)器");
    }

    private void transferFileHandle(String path, Socket client) {

        File fileToSend = new File(path);

        if (fileToSend.exists() && !fileToSend.isDirectory()) {
            try {
                PrintStream writer = new PrintStream(client.getOutputStream());
                writer.println("HTTP/1.0 200 OK");// 返回應(yīng)答消息,并結(jié)束應(yīng)答
                writer.println("Content-Type:application/binary");
                writer.println("Content-Length:" + fileToSend.length());// 返回內(nèi)容字節(jié)數(shù)
                writer.println();// 根據(jù) HTTP 協(xié)議, 空行將結(jié)束頭信息

                FileInputStream fis = new FileInputStream(fileToSend);
                byte[] buf = new byte[fis.available()];
                fis.read(buf);
                writer.write(buf);
                writer.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

項(xiàng)目地址:https://github.com/wohuifude1...

dpm-socket目錄下main目錄下SocketHttpServer.java文件

圖片描述

圖片描述

回答
編輯回答
奧特蛋

不是無法啟動,而是運(yùn)行完正常退出了,new 出來的Thread 要調(diào)用join方法阻塞主線程,要不主線程(main 方法)執(zhí)行完就退出了,等等剛才圖沒刷出來

2017年5月16日 12:27