Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import javax.imageio.ImageIO;
- public class DummyHttpWorker implements Runnable {
- private Socket s; // connection socket
- public DummyHttpWorker(Socket soc) {
- this.s = soc;
- }
- public void inviaRisposta(PrintWriter out)
- {
- out.println("HTTP/1.0 404 NOT FOUND");
- out.println("Server: Pippo");
- out.println("Date: "+ (new Date().toString()));
- out.println("Content-type: text/html");
- //out.println("Content-length: 0");
- out.println(); // blank line before message body..
- out.println("<!DOCTYPE HTML PUBLIC '-//IETF//DTD HTML 2.0//EN'>");
- out.println("<html><head><title>404 Not Found</title></head><body>");
- out.println("<h1>Pippo server says: Not Found!</h1>");
- out.println("<p>The requested URL was not found on this server.</p>");
- out.println("<hr></body></html>");
- }
- public void run()
- {
- PrintWriter out;
- BufferedReader in;
- System.out.println ("Connection successful! Waiting for input..");
- try {
- out = new PrintWriter(s.getOutputStream(),true);
- // PrintWriter permette di stampare su un output stream
- in = new BufferedReader(new InputStreamReader(s.getInputStream()));
- // BufferedReader legge testo da input stream e permette di effettuare operazioni di lettura caratteri e righe
- String inputLine, method = null, res = null;
- while ((inputLine = in.readLine()) != null)
- {
- System.out.println ("Received "+inputLine.length()+" chars: " + inputLine);
- // Check if there's a blank line: new line or carriage return as first char
- String[] data = inputLine.split(" ");
- //Nel caso la richiesta sia GET
- if(data[0].equals("GET")) {
- method = "GET";
- res = data[1];
- }
- if(inputLine.length() == 0) {
- out.println("HTTP/1.0 200 OK");
- out.println("Server: GX8Server");
- out.println("Date: "+ (new Date().toString()));
- //Se richiede / restituisco l'index
- if(res.equals("/"))
- sendRes("index.html", out);
- else{
- //Apro il file
- File f = new File(res.substring(1));
- String filename = f.getName();
- //Controllo che il file esista
- if(f.exists() && !f.isDirectory()) {
- //Controllo se è un'immagine
- if(isImage(res)) {
- //Mando l'header per specificare che è un'immagine e il tipo
- out.println("Content-type: image/"+filename.substring(filename.lastIndexOf(".")+1));
- out.println();
- OutputStream os = s.getOutputStream();
- sendImage(f,os); //Metodo per mandare immagini, vedi sotto
- }
- if(f.getName().endsWith(".html") || f.getName().endsWith(".txt")) //Controllo se è un file html o txt
- sendRes(filename, out); //Metodo per mandare file txt/html, vedi sotto
- }
- }
- break;
- }
- }
- System.out.println("Closing connection..");
- out.close();
- in.close();
- } catch (IOException ioe) {
- System.out.println("IOException on socket: " + ioe);
- ioe.printStackTrace();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- s.close();
- } catch (IOException ioe) {
- System.out.println("IOException on socket: " + ioe);
- ioe.printStackTrace();
- }
- }
- //Controlla se il file è un'immagine
- private boolean isImage(String estensione) {
- return (estensione.endsWith(".jpg")||estensione.endsWith(".png") || estensione.endsWith(".gif"));
- }
- //Funzione per mandare file txt e html
- private void sendRes(String res, PrintWriter out) throws Exception {
- out.println("Content-type: text/html");
- out.println();
- FileInputStream fin = new FileInputStream(res);
- while(fin.available()!=0){
- int info= fin.read();
- out.write(info);
- }
- }
- //Funzione per mandare immagini
- private void sendImage(File f, OutputStream out) throws IOException {
- String filename = f.getName();
- String estensione = filename.substring(filename.lastIndexOf(".")+1);
- BufferedImage buffImg = ImageIO.read(f);
- ImageIO.write(buffImg, estensione, out);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement