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(clientSocket.getOutputStream())), 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 != -1;
      pw.print((char) i);

    pw.println(" ");          // print a blank line for the header
    pw.println(" ");          // print a blank line for the header

  } catch (IOException e) {
    System.err.println("Could not open or read file " + fileName);
  }, 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 in the URL bar, it will just place the local file (i.e. - C:\MyServer\webpage\index.html) 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 :) )


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think your problem could be that PrintWriter sends chars, wheras you are sending binary data...

Can you just use the OutputStreamWriter you declare in your first line...
ctjoumasAuthor Commented:
I tried that - no go...
bah :-(

Maybe this can's ugly as sin, but it's supposed to work :-(
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ctjoumasAuthor Commented:
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 :)

Cool :-)  Glad I could point you in the right direction :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.