问题
Wrote a server that serves pages dynamically as well as static pages that are loaded into a page array on startup of the server. The server serves pages fine so the delivery is working.
The images are pulled from a database put into an image array when the page is requested with a random name put in the src attribute of the img tag and served from the image array when requested by the random name. They are then taken out of the image array so they are only accessed once as a security feature. The images are entering and leaving the image array and image is making it to the browser. The browser (Firefox) displays a message like: "The image cannot be displayed because it contains errors." Also, the output stream is being flushed so I don't think that it has anything to do with that.
Before they are put into the image array, the header is encoded to look like this (\n) inserted for extra clarity:
HTTP/1.1 200 OK
Content-Length: 18803
Content-Encoding: base64
Content-Type: image/png;"/2033.png"(\n\n)
And the rest would be the image in bytes. It is sent to the browser as bytes in the same way the pages are currently being sent. I have tried multiple variations of the above lines with different values, etc.
Any ideas on what I am doing wrong?
回答1:
Ok.. It turns out I was using \r\n in the wrong order.
This is what I had:
final private String newLine = "\n\r";
This is what I should have had:
final private String newLine = "\r\n";
This was causing an extra space in the wrong place which the html page was forgiving of but the image was not. Hope this is helpful to someone.
来源:https://stackoverflow.com/questions/12299277/browser-not-recognizing-image-sent-by-home-brew-server-in-java