ctjoumas
asked on
Java WebServer
I am writing a java webserver. Right now, my server can serve up htm/html files with no problem. Even .css files inside seem to work (although sometimes when I refresh the page, the css doesnt' take effect). I have one big quesiton...and a smaller question.
First question (the big one) is - my images are not showing up in the browser. The way I am printing the requested files (the html files and all the files that are used in the html code) to the browser is by using this code:
// first my pw var is set like this
output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(clientS ocket.getO utputStrea m())), true);
...
PrintWriter pw = new PrintWriter(output, true);
...
try {
/**
* file input stream for the file requested by the client
*/
FileInputStream fis = new FileInputStream(fileName);
for (int i=fis.read(); i != -1; i=fis.read())
pw.print((char) i);
pw.println(" "); // print a blank line for the header
pw.println(" "); // print a blank line for the header
fis.close();
} catch (IOException e) {
System.err.println("Could not open or read file " + fileName);
return;
}
Ok...so, that works fine. Now, I figured that the reason an image (say a jpg file) isn't being displayed is because it isn't a FileInputStream, but rather a FileImageInputStream. So, what I did was add another case that if a jpg (i have other files, but i used a jpg file as a test) file was being requested, I'd basically do the same code above, except I'd use a FileImageInputStream. This didn't work either. Anyone have any suggestions?!?!
My 2nd question was just a question of curiousity... When I start my server, it works fine in Netscape when i use http://localhost:8080/ and it also works fine when i enter http://my_ip:8080/ (of course 8080 is the port I'm running the server on). Now, when I use IE, http://localhost:8080/ will just redirect to the actual files...so in the URL bar, it will just place the local file (i.e. - C:\MyServer\webpage\index. html)...so of course it displays it all correctly because the browser is just reading the straight files...the server isn't doing anything. When I put in http://my_ip:8080/, it says page not found. So, I was curious about why IE doesn't work and why Netscape does (not a biggie..just curious...maybe IE just sucks :) )
Thanks!
Chris
First question (the big one) is - my images are not showing up in the browser. The way I am printing the requested files (the html files and all the files that are used in the html code) to the browser is by using this code:
// first my pw var is set like this
output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(clientS
...
PrintWriter pw = new PrintWriter(output, true);
...
try {
/**
* file input stream for the file requested by the client
*/
FileInputStream fis = new FileInputStream(fileName);
for (int i=fis.read(); i != -1; i=fis.read())
pw.print((char) i);
pw.println(" "); // print a blank line for the header
pw.println(" "); // print a blank line for the header
fis.close();
} catch (IOException e) {
System.err.println("Could not open or read file " + fileName);
return;
}
Ok...so, that works fine. Now, I figured that the reason an image (say a jpg file) isn't being displayed is because it isn't a FileInputStream, but rather a FileImageInputStream. So, what I did was add another case that if a jpg (i have other files, but i used a jpg file as a test) file was being requested, I'd basically do the same code above, except I'd use a FileImageInputStream. This didn't work either. Anyone have any suggestions?!?!
My 2nd question was just a question of curiousity... When I start my server, it works fine in Netscape when i use http://localhost:8080/ and it also works fine when i enter http://my_ip:8080/ (of course 8080 is the port I'm running the server on). Now, when I use IE, http://localhost:8080/ will just redirect to the actual files...so in the URL bar, it will just place the local file (i.e. - C:\MyServer\webpage\index.
Thanks!
Chris
ASKER
I tried that - no go...
bah :-(
Maybe this can help....it's ugly as sin, but it's supposed to work :-(
Maybe this can help....it's ugly as sin, but it's supposed to work :-(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! This worked out... it's not *quite* working yet, but my images are showing up...its crashing on some other images, but I will get it going :)
Chris
Chris
Cool :-) Glad I could point you in the right direction :-)
Can you just use the OutputStreamWriter you declare in your first line...