0

Attempting to build a web server from scratch. It's working for html, and other plain text files. Tested in Firefox and Chrome. But I'm having difficulty getting images to display properly. Status 200 returned, in Firefox I get the attached image on request. Other than the response header, do I need to handle image filetypes differently with the printstream?

enter image description here

FileInputStream fileIn = new FileInputStream("test/" +
                file);

// split specified file path
String[] pathTokens = tokens[1].split("/|\\.");
String fileExt = pathTokens[tokens.length - 1];

// build response
out.println("HTTP/1.1 200 OK");
if(fileExt.equals("jpg") || fileExt.equals("jpeg") ||fileExt.equals("png") || 
   fileExt.equals("ico"))
  out.println("Content type: image/" + fileExt);
else
  out.print("Content type: text/");
if(fileExt.equals("html"))
  out.println(fileExt);
else
  out.println("plain");

long size = fileIn.getChannel().size();

out.println("Content-Length: " + Long.toString(size));
out.println("Connect: Close");
out.println("");

int read = 0;
byte[] buffer = new byte[1024];

while ((read = fileIn.read(buffer)) != -1){
  out.write(buffer, 0, read);
}

If any further information is needed, please request. I'm more than willing to provide any additional details, or answer any questions I can.

EDIT: Upon further examination I'm actually getting 404 in Firefox, and I'm not really sure what Chrome is, the page displayed is "This site cannot be reached...", but it appears I'm getting 200 returned.

12
  • 1
    if(fileExt.equals("html")) should be else if(fileExt.equals("html")) (or some how attached to your previous else... basically, use braces). Also, why are you doing this? May 28, 2017 at 0:09
  • The line terminator should be \r\n, not whatever println() gives you.
    – user207421
    May 28, 2017 at 0:40
  • @ElliottFrisch Sorry preemptive comment earlier. You are correct thought, that was a problem. The other half of my issue wasn't shown in the selected code.
    – user1327900
    May 28, 2017 at 0:52
  • @EJP Both maybe acceptable then, because I currently have it working and I'm not using \r\n.
    – user1327900
    May 28, 2017 at 0:54
  • 2
    It doesn't matter what's acceptable. What matters is what is right. The RFC philosophy is 'be conservative in what you send and liberal in what you accept'. Ergo you should send only what's correct.
    – user207421
    May 28, 2017 at 1:42

1 Answer 1

0

I think I can see a bug:

if(fileExt.equals("jpg") || fileExt.equals("jpeg") ||fileExt.equals("png") || 
   fileExt.equals("ico"))
  out.println("Content type: image/" + fileExt);
else
  out.print("Content type: text/");
if(fileExt.equals("html"))
  out.println(fileExt);
else
  out.println("plain");

If I re-indent and insert {}'s where the Java compiler thinks they would be.

if (fileExt.equals("jpg") || fileExt.equals("jpeg") || 
    fileExt.equals("png") || fileExt.equals("ico")) {
    out.println("Content type: image/" + fileExt);
} else {
    out.print("Content type: text/");
}
if (fileExt.equals("html")) {
    out.println(fileExt);
} else {
    out.println("plain");
}

See the problem? If the extension is (say) "jpg", you will get a spurious "plain" line added to the header.

I think you actually meant to write this:

if (fileExt.equals("jpg") || fileExt.equals("jpeg") || 
    fileExt.equals("png") || fileExt.equals("ico")) {
    out.println("Content type: image/" + fileExt);
} else {
    out.print("Content type: text/");
    if (fileExt.equals("html")) {
        out.println(fileExt);
    } else {
        out.println("plain");
    }
}

This illustrates:

  1. The importance of getting making the indentation of your code match what the code actually says. (Hint: use your IDE's auto-indentation functionality!)
  2. That you should get into the habit of always using curly bracket blocks with all control constructs.
  3. Reinventing the wheel is not a good idea. Unless you like fixing broken wheels.
2
  • Yes. This is correct, it had been mentioned above. Thank you for posting an answer though. So it is clear to any who are curious.
    – user1327900
    May 28, 2017 at 2:16
  • In future, if you have your answer from the comments, it could be a good idea to delete the question .....
    – Stephen C
    May 28, 2017 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.