0

I am reading the file from the database and trying to open the file in browser. My code is below...

when I am doing like this at that time file is automatically downloaded

response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "inline; filename=\""+ docName + "\"");

But when I am doing like this at that time file is opening in browser.

response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=\""+ docName + "\"");

It is happening due to mimetype "application/pdf". But in my application I don't know the file's mimetype. It can be anything like pdf, image, txt, doc etc. So, I want to keep my mimetype default that is "application/octet-stream" and I also want to open that file in browser.

Please help me out.

Thanks in advance...

abhishek
  • 51
  • 2
  • 11

1 Answers1

0

I would give Apache Tika a try:

Getting MimeType subtype with Apache tika

or MediaType from the Guava libraries.

Without the content type the browser will download it as an attachment.

Community
  • 1
  • 1
Claes Mogren
  • 2,126
  • 1
  • 26
  • 34
  • If I don't set the actual content type then the file is not going to to open in browser automatically. Is not possible to show the file in browser without putting actual content type? – abhishek Oct 21 '14 at 12:05
  • No, I don't think so. I never got it to work in our application without setting the content type. We set it according to the file suffix since we know the original file name of the uploaded file. – Claes Mogren Oct 21 '14 at 13:12