I am trying to make a Java servlet that will download a file to be opened in a browser window (or downloaded if the browser can't view it). As an example, I'm trying to fetch and view a PNG image in my browser.
I tried sending a request with no headers (except Host: localhost
) via Fiddler, and I got back the image I was looking for.
When I try to view the image in my browser with the same URL, I get an invalid (transparent) image back. I tried adding the headers from my browser (Chrome) to Fiddler, and it looks like it fails when I add the following header: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
When I remove that header, or change it to Accept: */*
, the image loads again. I looked at the raw response in Fiddler and found there is a small difference in the actual data sent back.
This is what my servlet's code looks like. I'm not manually checking the Accept header for anything:
byte[] data = getData();
response.setContentType("image/png");
response.setHeader("Content-disposition", "inline; filename=\"my_image.png\"");
response.setContentLength(data.length);
OutputStream output = response.getOutputStream();
output.write(data);
output.close();
Why does Chrome's default Accept header make my response unreadable? How can I make my servlet ignore that header and send back the same data every time?
EDIT:
This is what Fiddler shows in the "Raw" view when I request the servlet without the Accept header (good):
HTTP/1.1 200 OK
x-xr-bookmark: ad70ff81-7415-4d46-954d-b79f98056729
content-disposition: inline; filename="image.PNG"
content-type: image/png
content-length: 82726
date: Tue, 21 Mar 2017 19:49:32 GMT
connection: close
PNG
IHDR 5- sRGB gAMA
a pHYs t t fx IDATx^ {p\ } &M $mg I K; N3 t`
*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***
And this is what Fiddler shows when I request it with the Accept header mentioned above (bad):
HTTP/1.1 200 OK
x-xr-bookmark: 5a1b1e6d-6b68-42ac-a2cb-545811f9a879
content-disposition: inline; filename="image.PNG"
content-type: image/png
date: Tue, 21 Mar 2017 19:49:46 GMT
connection: close
Transfer-Encoding: chunked
258ec
PNG
IHDR 5- sRGB gAMA
a pHYs t t fx IDATx^ {p\ } &M $mg I K; N3
*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***