简体   繁体   English

Java Web服务器src无法正常工作

[英]java web server src doesn't work

i have written a web server in java. 我已经用Java编写了Web服务器。 It is so simple and its job is to answer http requests on 8181 port. 它是如此简单,其工作是在8181端口上回答http请求。 Let's have a look on code: 让我们看一下代码:

public  void start() throws IOException {
    ServerSocket listener = new ServerSocket(8181,1000);
    System.out.println("Webserver starting up on port 8181");
    System.out.println("Waiting for connection");

    //==========================regex
    String pathRegex = "(?<=GET /).+?(?= HTTP)";

    Pattern pathPattern = Pattern.compile(pathRegex);
    //==========================/regex

    try {
        while (true) {
            Socket socket = listener.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));

            String requestHeaders = in.readLine();

            Matcher pathMatcher = pathPattern.matcher(requestHeaders);

            Boolean anyPath = pathMatcher.find();

            System.out.println("Connection, sending data to request : " + requestHeaders);
            try {
                PrintWriter out =
                    new PrintWriter(socket.getOutputStream(), true);
                if (!anyPath) {
                    JustHello(out);
                }
                else {
                    String directory = pathMatcher.group();

                    System.out.println(directory);

                    if (directory.toString().equals("pic")) {
                        JustPic(out);
                    }
                }
            } finally {
                socket.close();
            }
        }
    }
    finally {
        listener.close();
    }
}

private void JustHello(PrintWriter out) {
    // TODO Auto-generated method stub
    out.println("HTTP/1.0 200 OK");
    out.println("Content-Type: text/html");
    out.println("Server: Bot");
    // this blank line signals the end of the headers
    out.println("");
    out.flush();
    // Send the HTML page
    out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>Just Hello</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\nHello World\n</body>\n</html>");
    out.flush();
}

private void JustPic(PrintWriter out) {
    // TODO Auto-generated method stub
    out.println("HTTP/1.0 200 OK");
    out.println("Content-Type: text/html");
    out.println("Server: Bot");
    // this blank line signals the end of the headers
    out.println("");
    out.flush();
    // Send the HTML page
    out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>Just Hello</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\n<img width=\"300px\" height=\"200\" src=\"file:///C:/ccleverJavaWebServer/images/th.jpg\"/>\n</body>\n</html>");
    out.flush();
}

now the problem is that, the path of the file is ok but it does't work. 现在的问题是,该文件的路径可以,但是不起作用。 after loading the page with this " http://localhost:8181/pic " every thing ok but the img has't any image loaded! 在使用“ http://localhost:8181/pic ”加载页面后,一切正常,但是img没有加载任何图像!

what is the problem? 问题是什么?

You can't embed a local picture like that due to security issues. 由于安全问题,您无法嵌入这样的本地图片。 Browsers simply don't allow this behavior. 浏览器根本不允许这种行为。 See this question . 看到这个问题

SOLVED: to myself ;) You must make a way to answer file requests and move your files near your project source. 已解决:对我自己;)您必须设法答复文件请求并将文件移到项目源附近。 then make your addressing relative. 然后使您的称呼相对。 This is my code which works fine for me: 这是我的代码,对我来说很好:

package ir.cclever.example;

import java.awt.List;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WellCloseWebServer {

    public class clientInfo{
        public String ip;
        public Date visitTime;
    }

    static ArrayList<clientInfo> clients = new ArrayList<clientInfo>();

    public  void start() throws IOException {
        ServerSocket listener = new ServerSocket(8181,1000);
        System.out.println("Webserver starting up on port 8181");
        System.out.println("Waiting for connection");

        //==========================regex
        String pathRegex = "(?<=GET /).+?(?= HTTP)";

        Pattern pathPattern = Pattern.compile(pathRegex);
        //==========================/regex

        try {
            while (true) {
                Socket socket = listener.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

                String wholeRequest = "";
                String line;

                String requestHeaders = in.readLine();
                wholeRequest = wholeRequest.concat(requestHeaders + "\n<br>");

                while(!(line = in.readLine()).equals("")){
                    wholeRequest = wholeRequest.concat(line + "\n<br>");
                }

                wholeRequest = wholeRequest.concat("\n<br/>\n<br/>\nClient IP Address : " + socket.getRemoteSocketAddress().toString());
                wholeRequest = wholeRequest.concat("\n<br/>\n<br/>\nClient Opened Port : " + ((Integer)socket.getPort()).toString());

                clientInfo newClient = new clientInfo();
                newClient.ip = socket.getRemoteSocketAddress().toString();
                newClient.visitTime = new Date();
                clients.add(newClient);

                Matcher pathMatcher = pathPattern.matcher(requestHeaders);

                Boolean anyPath = pathMatcher.find();

                System.out.println("Connection, sending data to request : " + requestHeaders);
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                    if (!anyPath) {
                        JustHello(out);
                    }
                    else {
                        String directory = pathMatcher.group();

                        System.out.println(directory);

                        if (directory.toString().equals("pic")) {
                            JustPic(out);
                        }
                        else if(directory.toString().startsWith("server/")){
                            getFile(directory,dos,true);
                        }
                        else if(directory.toString().endsWith("jpg")){
                            getFile(directory,dos,false);
                        }
                        else if(directory.toString().equals("my.aspx")){
                            mirror(out,wholeRequest);
                        }
                        else if(directory.toString().equals("clients.html")){
                            WholeClients(out);
                        }
                        else{
                            errorPage(out);
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally {
                    socket.close();
                }
            }
        }
        finally {
            listener.close();
        }
    }

    private void InvalidRequestMethod(PrintWriter out, String string) {
        // TODO Auto-generated method stub
        out.println("HTTP/1.0 404 NOT FOUND");
        out.println("Content-Type: text/html");
        out.println("Server: Bot");
        // this blank line signals the end of the headers
        out.println("");
        out.flush();
        // Send the HTML page
        out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>Just GET</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\nInvalid request mehtod! : "+ string +"\n</body>\n</html>");
        out.flush();
    }

    private void errorPage(PrintWriter out) {
        // TODO Auto-generated method stub
        out.println("HTTP/1.0 404 NOT FOUND");
        out.println("Content-Type: text/html");
        out.println("Server: Bot");
        // this blank line signals the end of the headers
        out.println("");
        out.flush();
        // Send the HTML page
        out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>Error 404 NOT FOUND!</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\nContent not found on server!\n</body>\n</html>");
        out.flush();
    }

    private void WholeClients(PrintWriter out) {
        // TODO Auto-generated method stub
        out.println("HTTP/1.0 200 OK");
        out.println("Content-Type: text/html");
        out.println("Server: Bot");
        // this blank line signals the end of the headers
        out.println("");
        out.flush();
        // Send the HTML page
        out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>Whole Clients</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\n");
        Integer i=0;
        for (clientInfo item : clients) {
            i++;
            out.print("<br/>\n"+i.toString()+") ip : " + item.ip + " | visit time : " + item.visitTime.toString());
        }
        out.print("\n</body>\n</html>");
        out.flush();
    }

    private void mirror(PrintWriter out, String requestHeaders) {
        // TODO Auto-generated method stub
        out.println("HTTP/1.0 200 OK");
        out.println("Content-Type: text/html");
        out.println("Server: Bot");
        // this blank line signals the end of the headers
        out.println("");
        out.flush();
        // Send the HTML page
        out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>My Info</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\n" +
                requestHeaders.toString() +
                "\n</body>\n</html>");
        out.flush();
    }

    private void getFile(String directory, DataOutputStream os, boolean b) {
        String CRLF = "\r\n";
        // Open the requested file.
        String fileName = directory;
        FileInputStream fis = null;
        boolean fileExists = true;
        try {
        //(new File(fileName)).mkdirs();
        fis = new FileInputStream(fileName);
        } catch (FileNotFoundException e) {
        fileExists = false;
        }

        // Construct the response message.
        String statusLine = null;
        String contentTypeLine = null;
        String entityBody = null;
        if (fileExists) {
        statusLine = "200 OK" + CRLF;
        contentTypeLine = "Content-type: " + 
                contentType( fileName ) + CRLF;
        } else {
        statusLine = "404 NOT FOUND" + CRLF;
        contentTypeLine = "Content Not Found!" + CRLF;
        entityBody = "<HTML>" + 
            "<HEAD><TITLE>Not Found</TITLE></HEAD>" +
            "<BODY>Not Found</BODY></HTML>";
        }

        // Send the status line.
        try {
            if (b) {
                os.writeBytes(statusLine);
                // Send the content type line.
                os.writeBytes(contentTypeLine);
                // Send a blank line to indicate the end of the header lines.
                os.writeBytes(CRLF);
            }
            // Send the entity body.
            if (fileExists) {
            sendBytes(fis, os);
            fis.close();
            } else {
            os.writeBytes("File DNE: Content Not Found!");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String contentType(String fileName) {
        if(fileName.endsWith(".htm") || fileName.endsWith(".html")) {
            return "text/html";
        }
        if(fileName.endsWith(".jpeg") || fileName.endsWith(".jpg")) {
        return "image/jpeg";
        }
        if(fileName.endsWith(".gif")) {
        return "image/gif";
        }
        if(fileName.endsWith(".png")) {
            return "image/png";
            }
        if(fileName.endsWith(".js")) {
            return "text/javascript";
            }
        if(fileName.endsWith(".css")) {
            return "text/stylesheet";
            }

        return "application/octet-stream";
        }

    private static void sendBytes(FileInputStream fis, OutputStream os) 
        throws Exception {
        // Construct a 1K buffer to hold bytes on their way to the socket.
        byte[] buffer = new byte[1024];
        int bytes = 0;

        // Copy requested file into the socket's output stream.
        while((bytes = fis.read(buffer)) != -1 ) {
            os.write(buffer, 0, bytes);
            }
        }

    private void JustHello(PrintWriter out) {
        // TODO Auto-generated method stub
        out.println("HTTP/1.0 200 OK");
        out.println("Content-Type: text/html");
        out.println("Server: Bot");
        // this blank line signals the end of the headers
        out.println("");
        out.flush();
        // Send the HTML page
        out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>Just Hello</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\nHello World\n</body>\n</html>");
        out.flush();
    }

    private void JustPic(PrintWriter out) {
        // TODO Auto-generated method stub
        out.println("HTTP/1.0 200 OK");
        out.println("Content-Type: text/html");
        out.println("Server: Bot");
        // this blank line signals the end of the headers
        out.println("");
        out.flush();
        // Send the HTML page
        out.print("<!DOCTYPE html>\n<html>\n<head>\n<title>Just Pic</title>\n<meta charset=\"UTF-8\">\n</head>\n<body>\n<img src=\"/images/th.jpg\"/>\n</body>\n</html>");
        out.flush();
    }
}

see the if clause that responds to the requests that start with /server. 查看响应以/ server开头的请求的if子句。 the follow the code to where it handles the request. 按照代码处理请求。 it returns the files as a file transporter. 它将文件作为文件传输器返回。 so the address here "server/image.jpg" will be a address that you give in regular programming in java. 因此,这里的“ server / image.jpg”地址将是您在Java常规编程中提供的地址。 this address is from the root of your project. 该地址来自项目的根目录。 mention that this code must have a main to use the class. 提到此代码必须具有主类才能使用该类。 this works perfect for me. 这对我来说很完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM