Reputation: 21
I m using content disposition to download pdf file from my servlet. My code works fine for chrome, firefox and IE but the problem is when I try to download pdf file using opera, it removes pdf extension and adds htm. The following is my code:
String filename = "abc.pdf";
String filepath = "/pdf/" + filename;
System.out.println("filepath "+filepath);
resp.addHeader("content-disposition", "attachment; filename=" + filename);
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream(filepath);
System.out.println(is.toString());
int read = 0;
byte[] bytes = new byte[1024];
OutputStream os = resp.getOutputStream();
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
System.out.println(read);
os.flush();
os.close();
}catch(Exception ex){
logger.error("Exception occurred while downloading pdf -- "+ex.getMessage());
System.out.println(ex.getStackTrace());
}
Upvotes: 0
Views: 3088
Reputation: 691715
You should probably set the content type of the response to application/pdf
, to let the browser know that the downloaded file is not a HTML file, but a PDF file.
See ServletResponse.setContentType()
.
Upvotes: 2