1

My problem is that I am not able to get non-text files to display properly in the browser. In the code for my server, I use a while loop to listen for requests. When a request comes in, I read the headers into a variable headers. After that, I check if the request is a GET request. If it is, then I check that the file exists and that it is a file and not a directory. Then, I look at the extension of the file to determine the next line in my HTTP response.

pw.print("HTTP/1.1 Status: 200 OK\r\n");
String ext = file.getName().substring( file.getName().lastIndexOf('.') + 1, file.getName().length() );
if(ext.equals("txt"))
{
    pw.print("Content-Type: text/plain\r\n\r\n");
}
else if(ext.equals("html"))
{
    pw.print("Content-Type: text/html\r\n\r\n");
}
else if(ext.equals("jpg"))
{
    pw.print("Content-Type: image/jpeg\r\n\r\n");
}
else if(ext.equals("png"))
{
    pw.print("Content-Type: image/png\r\n\r\n");
}
else if(ext.equals("pdf"))
{
    pw.print("Content-Type: application/pdf\r\n\r\n");
}

if( !ext.equals("txt") && !ext.equals("html") )
{
    ImageInputStream imageStream = ImageIO.createImageInputStream(file);
    BufferedImage bufferedImage = ImageIO.read(file);
    ImageIO.write(bufferedImage, ext, socket.getOutputStream());
}

Now, as far as I know once I get into the last conditional (I'm requesting localhost:port/my_png.png) then the content should be written to the socket and, because I told it what its Content-Type should be, the browser should be able to interpret that file. Unfortunately, that is not happening. I am getting text output in the browser that are bytes. My output looks something like this:

‰PNG
���
IHDR���i���`���‘ïË¥��%IDATxÚíœ=h#×úÆ]ÜBE

Does anyone know why I'm having this problem?

beznez
  • 1,883
  • 4
  • 17
  • 23
  • I do not know if it is the cause of the problem, but shouldn't your response have a content-length? – ThePerson Sep 13 '14 at 20:16
  • 1
    I would also recommend looking here for getting the content type, rather than having lots of if-else ifs: http://stackoverflow.com/questions/51438/getting-a-files-mime-type-in-java – ThePerson Sep 13 '14 at 20:18
  • Your code is not sufficient to tell where exactly the problem is, but a possible error: is `socket` the connection to the browser? if so, then it seems that you're not writing the headers before the body. Can you post a bit more of code? – morgano Sep 13 '14 at 20:34
  • Yes, `socket` is the connection to the browser. But, I'm writing the end of the headers in the top conditionals before I output the body in the last conditional. – beznez Sep 13 '14 at 20:38
  • 1
    How exactly are you getting `pw`? there's something here that doesn't look Ok... anyway, try flushing: `pw.flush()` before writing to `socket` – morgano Sep 13 '14 at 21:35
  • I'm sorry I didn't mention that. I should have. `PrintWriter pw = new PrintWriter(socket.getOutputStream(), true)`. I know that PrintWriter will only write text. That is why when I write to the socket with `ImageIO`, I use `socket.getOutputStream()`. – beznez Sep 14 '14 at 01:22

1 Answers1

0

@morgano: Yes, I needed to pw.flush() before I wrote to socket. Thanks very much.

beznez
  • 1,883
  • 4
  • 17
  • 23