Java WebServer

Posted on 2003-03-11
Medium Priority
Last Modified: 2008-03-06
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=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

  } catch (IOException e) {
    System.err.println("Could not open or read file " + fileName);
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 :) )


Question by:ctjoumas
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 35

Expert Comment

ID: 8112407
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...

Author Comment

ID: 8112622
I tried that - no go...
LVL 35

Expert Comment

ID: 8112806
bah :-(

Maybe this can help....it's ugly as sin, but it's supposed to work :-(
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 35

Accepted Solution

TimYates earned 250 total points
ID: 8112807

Author Comment

ID: 8120323
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 :)

LVL 35

Expert Comment

ID: 8120388
Cool :-)  Glad I could point you in the right direction :-)

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 15 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question