nightnightman
nightnightman

Reputation: 15

How to display an image/gif in a browser through a Java Web Server

I have a simple Java Web Server, which i am trying to use to display images in a browser.

so far i have it so that when going to localhost:7500/image2.jpg it downloads the image rather than displaying it in the browser

when going to a gif extension (localhost:7500/image33.gif) it just shows a tiny black square.

here is what i have done so far:

    public void getType(File f, String path, BufferedReader bfr)
    {
        String extention = path.substring(path.lastIndexOf("/") + 1);
        try {
        if (extention == "gif")
        {
            String line;
            String httpResponse = "HTTP/1.1";
            httpResponse += " 200 OK \n";
            httpResponse += "Content-Type: image/gif\n" ;
            httpResponse += "Content-Length: " + f.length()+"\n\n";

            serverClient.getOutputStream().write(httpResponse.getBytes("UTF-8"));

            //loop to print each line of file to browser
            while ((line = bfr.readLine()) != null) 
            {
                serverClient.getOutputStream().write(line.getBytes("UTF-8"));
            }
        }
        else if (extention == "jpg")
        {
            String line;
            String httpResponse = "HTTP/1.1";
            httpResponse += " 200 OK \n";
            httpResponse += "Content-Type: image/jpg\n" ;
            httpResponse += "Content-Length: " + f.length()+"\n\n";

            serverClient.getOutputStream().write(httpResponse.getBytes("UTF-8"));

            //loop to print each line of file to browser
            while ((line = bfr.readLine()) != null) 
            {
                serverClient.getOutputStream().write(line.getBytes("UTF-8"));
            }

        }
        else
        {
            String line;
            String httpResponse = "HTTP/1.1 200 OK\r\n\r\n";
            serverClient.getOutputStream().write(httpResponse.getBytes("UTF-8"));

            //loop to print each line of file to browser
            while ((line = bfr.readLine()) != null) 
            {
                serverClient.getOutputStream().write(line.getBytes("UTF-8"));
            }
        }
    }catch(Exception ex)
      {
      //when page is loaded, print confirmation to system
      System.out.println("999999999");
      }

    }

Upvotes: 0

Views: 624

Answers (1)

DuncG
DuncG

Reputation: 15186

By using a BufferedReader you will corrupt JPG/GIF files as this transforms a series of byte -> to char -> UTF-8 bytes by line by line. Instead you should be opening the GIF/JPG as InputStream, and write them directly to the servlet output stream unchanged. Get rid of these these loops:

//loop to print each line of file to browser
while ((line = bfr.readLine()) != null) 
{
   serverClient.getOutputStream().write(line.getBytes("UTF-8"));
}

Just use NIO Files to copy images directly without any transformation:

Files.copy(f.toPath(), serverClient.getOutputStream());

The third and final loop over bfr may or may not work depending on what the file represents. Your file might be character based (eg TXT) file, so writing line at a time calling BufferedReader readLine will work, and helpful if you set the character set on the HTTP servlet response. If the file is binary format eg MP3 etc then your 3rd loop will just corrupt the stream as for GIF/JPG.

Upvotes: 1

Related Questions