Getting the right image from my Java web server

I created a simple HTTP server in Java. When a browser sends a GET request to my web server for an image file, say .jpg. My browser is not currently receiving the image properly.

What header fields should be set?

Currently I have a date, server, content type, content length, connection. I set the length with:

fin = new FileInputStream(fileName);
contentLength = fin.available();

Content-Type is set to the correct MIME type, so there is no problem.

I am writing file data using:

public void sendFile (FileInputStream fin, DataOutputStream out) 
{
    byte[] buffer = new byte[1024];
    int bytesRead;
    int strCnt = 0;
    try
    {
        int cnt = 0;
        while ((bytesRead = fin.read(buffer)) != -1)
        {
             out.write(buffer, 0, bytesRead);
        }
        fin.close();
    }
    catch (IOException ex)
    {

    }
}

This is what my Chrome browser gets

Chrome get

The full length of the content does not seem to load.

The actual image file size is 2.73 KB.

If there are no header fields, what could be causing the problem?

+3
source share
1

, . out.flush(); out.close(); fin.close():

out.flush();
out.close();
fin.close();

DataOutputStream BufferedOutputStream. DataOutputStream hd/network.

+3

All Articles