Setting content type in Java for file download

64,921

Solution 1

Take a look at javax.activation.MimetypesFileTypeMap (comes as part of Java6, can be downloaded for prior version). It has a method that returns the mime type for a given filename.

I've never tried using it myself, though, and it's possible you'll still need to supply it with a list of mime types. Worth a look, though.

Solution 2

To giving a working example of what @skaffman listed when using a Servlet:

String fileName = "c:/temp/URL.txt";
MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap();
response.setContentType(mimetypesFileTypeMap.getContentType(fileName));
response.getOutputStream().write(Files.readAllBytes(Paths.get(fileName)));

Solution 3

URLConnection.getFileNameMap().getContentTypeFor(string) works for txt, pdf, avi; fails for doc, odt, mp3...

Solution 4

A FileTypeDetector class was added in Java 1.7 that allows for probing a file to guess its type.

Specifically, you can use Files.probeContentType. Some info here. And here's a code sample:

File f = new File("c:\\temp\\myFile.mp4");
resp.setHeader("Content-Disposition", "attachment; filename=\"" + f.getName() + "\"");
resp.setContentType(Files.probeContentType(f.toPath()));

Per the docs, probeContentType is highly implementation specific and may simply examine the file name, it may use a file attribute, or it may examines bytes in the file.

As skaffman answered, there's also MimetypesFileTypeMap, which in comparison provides data typing of files via their file extension.

Share:
64,921
coder247
Author by

coder247

Updated on May 07, 2021

Comments

  • coder247
    coder247 almost 3 years

    In my application I like to provide file download facility. I set the file types to response.setContentType. How can I set the content types for almost all known file types? Is there any easy way? or I need to set it manually like i do now, which is given below.

    if (pictureName.indexOf("jpg") > 0) {
       res.setContentType("image/jpg");
    } else if (pictureName.indexOf("gif") > 0) {
       res.setContentType("image/gif");
    } else if (pictureName.indexOf("pdf") > 0) {
       res.setContentType("application/pdf");
       res.setHeader("Content-Disposition", "inline; filename=\"" + pictureName + "\"");
    } else if (pictureName.indexOf("html") > 0) {
       res.setContentType("text/html");
    } else if (pictureName.indexOf("zip") > 0) {
       res.setContentType("application/zip");
       res.setHeader("Content-Disposition", "attachment; filename=\"" + pictureName + "\"");
    }
    
  • Kevin Reid
    Kevin Reid about 14 years
    Tested on my Mac OS X 10.5.8, Java 1.6.0_17, new MimetypesFileTypeMap().getContentType(filename) produces these results: x.jpg image/jpeg, x.jpeg image/jpeg, x.gif image/gif, x.pdf application/octet-stream, x.html text/html, x.zip application/octet-stream. [Ack, bad formatting as a comment. Should I post this as a separate answer, or someone who can edit put it in this one?]
  • skaffman
    skaffman about 14 years
    @Kevin: I'm not sure, are you supporting my answer, or disagreeing with it? :)
  • Kevin Reid
    Kevin Reid about 14 years
    Trying to provide data on how well this solution works. No opinion, really.