Click here to Skip to main content
16,002,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to implement a server application that is able to receive HTTP requests . Implement HTTP GET command , so that it can respond to requests for content and implement the status codes 200 and 404. The content should be stored on disk in file format and have an HTML page consists of 4 images.

I am beginner in implementing socket with programming and so far managed to read the header of the file and stores it , but I can not display it in the browser.

Java
public class WebServer {

    public static void main(String[] args) throws IOException {

        int porta = 9094;
        ServerSocket ss = new ServerSocket(porta);
        System.err.println("Connected to port server: " + porta);
        System.err.println("Waiting Customer Connect!");

        while (true){

            try (Socket client = ss.accept()) {
                System.err.println("New Client Connect");

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

                BufferedWriter out;
                out = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));

                String s;
                int x = 0;
                String temp = null;
                while ((s = in.readLine()) != null) {
                    System.out.println(s);
                    if (x==0){
                        temp = s;
                        x++;
                    }
                    if (s.isEmpty()) {
                        break;
                    }
                }

                out.write("HTTP/1.1 200 OK");
                out.write("\r\n");
                out.write("Content-Type: text/html");
                out.write("\r\n");
                out.write("\r\n");
                out.write("<!DOCTYPE html>");
                out.write("<title>WebServer</title>");
                out.write("<link rel='shortcut icon' href='http://findicons.com/icon/download/97152/circle_green/48/ico?id=97152'>");

                out.write("<html>");
                out.write("<body>");
                out.write("<h1>Home WebServer</h1>");
                out.write("<p>Successfully connected!</p>");
                out.write("<p><a href=\"/download.html\">1) Download</a></p>");
                out.write("<p><a href=\"/img1.png\">1) Imagem 1</a></p>");
                out.write("<br>");
                out.write("</body>");
                out.write("</html>");

                lerArquivo(guardaArquivo(temp));

                out.flush();

                System.err.println("Connection to the Client Closed!");
                out.close();
                in.close();
            }
        }
    }

    //Get the file name you want to open
    public static String guardaArquivo(String s) throws FileNotFoundException, IOException{

        String procuraEspaco[] = s.split(" ");
        String guarda = procuraEspaco[1];
        String procuraBarra[] = guarda.split("/");

        for (int i = 0; i < procuraBarra.length; i++) {
            if(procuraBarra[i] != null){
                //System.out.println("-> " + procuraBarra[i]);
                guarda = procuraBarra[i];
            }
        }
        return guarda;
    }

    //Passes as parameter the file name to read
    public static String lerArquivo(String nomearquivo) throws FileNotFoundException, IOException{
                String caminho = new File(".").getAbsolutePath();
                String caminhoCompleto = caminho + "\\" + nomearquivo;
                System.out.println(caminhoCompleto);
                BufferedReader br = new BufferedReader(new FileReader(caminhoCompleto));
                br.toString();

                StringBuilder sb = new StringBuilder();
                String line = br.readLine();

                while (line != null) {
                    sb.append(line);
                    sb.append("\n");
                    line = br.readLine();
                }
        return sb.toString();
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900