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(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)