Reputation: 665
I have created a simple HTTP server in Java. When the browser sends a GET request to my web server for a image file, let's say .jpg. Currently my browser does not get the image properly.
Exactly what header fields must be set?
Currently I have Date, Server, Content-type, Content-Length, Connection. I set the length by using:
fin = new FileInputStream(fileName);
contentLength = fin.available();
Content-Type is set to the correct mime-type, so no problem there.
I write the 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 is received by my Chrome Browser
It seems to not download the full content length.
The actual size of the image file is 2.73KB.
If no header fields are missing then what could be causing the problem?
Upvotes: 3
Views: 1836
Reputation: 5220
Look likes you do not send all data. Try to add out.flush(); out.close(); before fin.close():
out.flush();
out.close();
fin.close();
I also suggest you to wrap your DataOutputStream into BufferedOutputStream. From my practice it work much more faster comparing to DataOutputStream when writing to hd/network.
Upvotes: 3