I'm trying to understand a strange behaviur in pdf file download using spring mvc servlet.
Here is the controller code used to download files:
@RequestMapping(value = "/abc", method = RequestMethod.GET)
public HttpEntity<byte[]> report(@PathVariable("id") Long id,
HttpServletResponse response,
HttpServletRequest request) {
byte[] bytes = service.reportById(id);
return DownloadUtil.downloadFile(response, "application/pdf",
"Filename", bytes);
}
public static HttpEntity<byte[]> downloadFile(
final HttpServletResponse response,
final String contentType,
final String fileName,
final byte[] item){
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.valueOf(contentType));
header.set("Content-Disposition", "inline; filename=\"" + fileName +"\"");
header.set("Content-Transfer-Encoding", "application/octet-stream");
header.setContentLength(item.length);
return new HttpEntity<byte[]>(item, header);
}
When pdf is displayed in chrome viewer it works. Here request header:
=== MimeHeaders ===
host = localhost:8080
connection = keep-alive
cache-control = max-age=0
accept = text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
user-agent = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
accept-encoding = gzip, deflate, sdch
accept-language = it,en-US;q=0.8,en;q=0.6
cookie = JSESSIONID=XXX; _ga=GA1.1.2013320496.1416898514;
When I save that pdf I've this header:
=== MimeHeaders ===
host = localhost:8080
connection = keep-alive
referer = http://localhost:8080/xxxx
user-agent = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
accept-encoding = gzip, deflate, sdch
accept-language = it,en-US;q=0.8,en;q=0.6
cookie = JSESSIONID=XXX; _ga=GA1.1.2013320496.1416898514;
The main difference is in accept
header and in referer
, I think.
The problem is that, when this file is saved it have a wrong encoding and results to be damaged.
Right file content is like this:
%PDF-1.4
%\E2\E3\CF\D3
6 0 obj
....
Wrong file is something like:
"JVBERi0xLjQKJeLjz9MKMyAwIG9iago8PC9BbHRlcm5hdGUvRG
In botch cases I have the following response headers:
=== MimeHeaders ===
X-Content-Type-Options = nosniff
X-XSS-Protection = 1; mode=block
Cache-Control = no-cache, no-store, max-age=0, must-revalidate
Pragma = no-cache
Expires = 0
X-Frame-Options = DENY
The strange thing is that I use the ~same code in another project to do the same thing and it works. So I think, may be something in servlet config?
How can I force right download encoding?
Aucun commentaire:
Enregistrer un commentaire