Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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
  • 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 :-(

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

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!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

580 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