Hallo,
ich habe folgendes Listing von einem Buch abgeschrieben aber es geht nicht.
In der Methode "sendHeader" ist wohl der Fehler.
In der Zeile 140
Aber warum?
Ihr braucht noch irgendeine index.html die in Euren Arbeitsverzeichnis ist.
Nachdem Ihr das Programm aufgerufen habt, tipt Ihr auf Euren Internet-Browser: http://localhost:8080/
Es müßte dann der Inhalt der index.html auf dem Browser sein.
Zeile 28 habe ich ausgeklammert, da ich nicht genau weiß wie diese Datei aufgebaut sein muss, ist aber wohl nicht so wichtig.
ich habe folgendes Listing von einem Buch abgeschrieben aber es geht nicht.
In der Methode "sendHeader" ist wohl der Fehler.
In der Zeile 140
wird die IOException ausgelöst.out.writeBytes(key + ": " + myHeaders.get(key) + CRLF);
Aber warum?
Ihr braucht noch irgendeine index.html die in Euren Arbeitsverzeichnis ist.
Nachdem Ihr das Programm aufgerufen habt, tipt Ihr auf Euren Internet-Browser: http://localhost:8080/
Es müßte dann der Inhalt der index.html auf dem Browser sein.
Zeile 28 habe ich ausgeklammert, da ich nicht genau weiß wie diese Datei aufgebaut sein muss, ist aber wohl nicht so wichtig.
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class SimpleHttpd extends Thread
{
protected Socket s = null;
protected static File docRoot;
protected static String canonicalDocRoot;
public final static int HTTP_PORT = 8080;
public final static String CRLF = "\r\n";
public final static String PROTOCOL = "HTTP/1.0";
public final static String SC_OK = "200 OK";
public final static String SC_BAD_REQUEST = "400 Bad Request";
public final static String SC_FORBIDDEN = "403 Forbidden";
public final static String SC_NOT_FOUND = "404 Not Found";
protected String statusCode = SC_OK;
protected static Properties typeMap = new Properties();
protected Hashtable myHeaders = new Hashtable();
public static void main(String argv[])
{
try
{
//typeMap.load(new FileInputStream("mime.types"));
docRoot = new File(".");
canonicalDocRoot = docRoot.getCanonicalPath();
ServerSocket listen = new ServerSocket(HTTP_PORT);
while (true)
{
SimpleHttpd aRequest = new SimpleHttpd(listen.accept());
}
}
catch (IOException e)
{
System.err.println("Fehler : " + e.toString());
}
}
public SimpleHttpd(Socket s)
{
this.s = s;
start();
}
public void run()
{
try
{
setHeader("Server", "SimpleHttpd");
BufferedReader is = new BufferedReader( new InputStreamReader(s.getInputStream()));
DataOutputStream os = new DataOutputStream(s.getOutputStream());
String request = is.readLine();
System.out.println("Request: " + request);
StringTokenizer st = new StringTokenizer(request);
if ((st.countTokens()==3) && st.nextToken().equals("GET"))
{
String filename = docRoot.getPath() + st.nextToken();
if (filename.endsWith("/") || filename.equals(""))
filename += "index.html";
File file = new File(filename);
if(file.getCanonicalPath().startsWith(canonicalDocRoot))
sendDocument(os, file);
else
sendError(SC_FORBIDDEN, os);
}
else
sendError(SC_BAD_REQUEST, os);
is.close();
os.close();
s.close();
}
catch (IOException ioe)
{
System.err.println("Fehler:" + ioe.toString());
}
}
protected void sendDocument(DataOutputStream os, File file) throws IOException
{
try
{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
sendStatusLine(os);
setHeader("Content-Length", (new Long(file.length())).toString());
setHeader("Content-Type", guessType(file.getPath()));
sendHeader(os);
os.writeBytes(CRLF);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf, 0, 1024)) != -1)
{
os.write(buf, 0 , len);
}
in.close();
}
catch (FileNotFoundException fnfe)
{
sendError(SC_NOT_FOUND, os);
}
}
protected void setStatusCode(String statusCode)
{
this.statusCode = statusCode;
}
protected String getStatusCode()
{
return statusCode;
}
protected void sendStatusLine(DataOutputStream out) throws IOException
{
out.writeBytes(PROTOCOL + this.getStatusCode() + this.CRLF);
}
protected void setHeader(String key, String value)
{
myHeaders.put(key, value);
}
protected String getHeader(String key)
{
return (String)myHeaders.get(key);
}
protected void sendHeader(DataOutputStream out) throws IOException
{
String line;
String key;
Enumeration e = myHeaders.keys();
while(e.hasMoreElements())
{
key = (String)e.nextElement();
out.writeBytes(key + ": " + myHeaders.get(key) + CRLF);
}
}
protected void sendError(String statusCode, DataOutputStream out) throws IOException
{
setStatusCode(statusCode);
sendStatusLine(out);
out.writeBytes(
CRLF
+ "<html>"
+ "<head><title>" + getStatusCode() + "</title></head>"
+ "<body><h1>" + getStatusCode() + "</h1></body>"
+ "</html>"
);
System.err.println(getStatusCode());
}
public String guessType(String filename)
{
String type = null;
int i = filename.lastIndexOf(".");
if (i > 0)
type = typeMap.getProperty(filename.substring(i));
if (type == null)
type = "unknown/unknown";
return type;
}
}