Browser not recognizing image sent by home brew server in Java

早过忘川 提交于 2020-01-06 15:47:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!