超好用的两行java代码能获取几乎所有文件的contentType,返回前端能在浏览器上直接打开

这篇博客介绍了如何利用Java的NIO包以及通过文件扩展名,高效地获取文件的contentType,使得文件能在浏览器上直接打开。
摘要由CSDN通过智能技术生成
String contentType;
Path path = Paths.get(fileFullPath);//fileFullPath为文件路径(String)
contentType = Files.probeContentType(path); 
response.setContentType(contentType);
response.setHeader("Content-Disposition", "inline; filename=" + urlEncodedFileName);

上面是nio包提供的方法

或者根据文件扩展名获取,这样没牵扯到 IO效率可能会高一点

public class HttpUtil {

   private static Map<String , String> contentTypeMap = new HashMap<>();
    public static String getContentType(String fileName) {//也可以是文件路径

        String contentType = "application/octet-stream"; //不知道的类型

        if (fileName.lastIndexOf(".") < 0) //获取不到扩展名
           return contentType;

        fileName = fileName.toLowerCase(); 
        String ext = fileName.substring(fileName.lastIndexOf("."));
       return contentTypeMap.get(ext);
    }

    static {
        contentTypeMap.put(".aac" , "audio/mp4");
        contentTypeMap.put(".load" , "text/html");
        contentTypeMap.put(".123" , "application/vnd.lotus-1-2-3");
        contentTypeMap.put(".3ds" , "image/x-3ds");
        contentTypeMap.put(".3g2" , "video/3gpp");
        contentTypeMap.put(".3ga" , "video/3gpp");
        contentTypeMap.put(".3gp" , "video/3gpp");
        contentTypeMap.put(".3gpp" , "video/3gpp");
        contentTypeMap.put(".602" , "application/x-t602");
        contentTypeMap.put(".669" , "audio/x-mod");
        contentTypeMap.put(".7z" , "application/x-7z-compressed");
        contentTypeMap.put(".a" , "application/x-archive");
        contentTypeMap.put(".abw" , "application/x-abiword");
        contentTypeMap.put(".abw.crashed" , "application/x-abiword");
        contentTypeMap.put(".abw.gz" , "application/x-abiword");
        contentTypeMap.put(".ac3" , "audio/ac3");
        contentTypeMap.put(".ace" , "application/x-ace");
        contentTypeMap.put(".adb" , "text/x-adasrc");
        contentTypeMap.put(".ads" , "text/x-adasrc");
        contentTypeMap.put(".afm" , "application/x-font-afm");
        contentTypeMap.put(".ag" , "image/x-applix-graphics");
        contentTypeMap.put(".ai" , "application/illustrator");
        contentTypeMap.put(".aif" , "audio/x-aiff");
        contentTypeMap.put(".aifc" , "audio/x-aiff");
        contentTypeMap.put(".aiff" , "audio/x-aiff");
        contentTypeMap.put(".al" , "application/x-perl");
        contentTypeMap.put(".alz" , "application/x-alz");
        contentTypeMap.put(".amr" , "audio/amr");
        contentTypeMap.put(".ani" , "application/x-navi-animation");
        contentTypeMap.put(".anim[1-9j]" , "video/x-anim");
        contentTypeMap.put(".anx" , "application/annodex");
        contentTypeMap.put(".ape" , "audio/x-ape");
        contentTypeMap.put(".arj" , "application/x-arj");
        contentTypeMap.put(".arw" , "image/x-sony-arw");
        contentTypeMap.put(".as" , "application/x-applix-spreadsheet");
        contentTypeMap.put(".asc" , "text/plain");
        contentTypeMap.put(".asf" , "video/x-ms-asf");
        contentTypeMap.put(".asp" , "application/x-asp");
        contentTypeMap.put(".ass" , "text/x-ssa");
        contentTypeMap.put(".asx" , "audio/x-ms-asx");
        contentTypeMap.put(".atom" , "application/atom+xml");
        contentTypeMap.put(".au" , "audio/basic");
        contentTypeMap.put(".avi" , "video/x-msvideo");
        contentTypeMap.put(".aw" , "application/x-applix-word");
        contentTypeMap.put(".awb" , "audio/amr-wb");
        contentTypeMap.put(".awk" , "application/x-awk");
        contentTypeMap.put(".axa" , "audio/annodex");
        contentTypeMap.put(".axv" , "video/annodex");
        contentTypeMap.put(".bak" , "application/x-trash");
        contentTypeMap.put(".bcpio" , "application/x-bcpio");
        contentTypeMap.put(".bdf" , "application/x-font-bdf");
        contentTypeMap.put(".bib" , "text/x-bibtex");
        contentTypeMap.put(".bin" , "application/octet-stream");
        contentTypeMap.put(".blend" , "application/x-blender");
        contentTypeMap.put(".blender" , "application/x-blender");
        contentTypeMap.put(".bmp" , "image/bmp");
        contentTypeMap.put(".bz" , "application/x-bzip");
        contentTypeMap.put(".bz2" , "application/x-bzip");
        contentTypeMap.put(".c" , "text/x-csrc");
        contentTypeMap.put(".c++" , "text/x-c++src");
        contentTypeMap.put(".cab" , "application/vnd.ms-cab-compressed");
        contentTypeMap.put(".cb7" , "application/x-cb7");
        contentTypeMap.put(".cbr" , "application/x-cbr");
        contentTypeMap.put(".cbt" , "application/x-cbt");
        contentTypeMap.put(".cbz" , "application/x-cbz");
        contentTypeMap.put(".cc" , "text/x-c++src");
        contentTypeMap.put(".cdf" , "application/x-netcdf");
        contentTypeMap.put(".cdr" , "application/vnd.corel-draw");
        contentTypeMap.put(".cer" , "application/x-x509-ca-cert");
        contentTypeMap.put(".cert" , "application/x-x509-ca-cert");
        contentTypeMap.put(".cgm" , "image/cgm");
        contentTypeMap.put(".chm" , "application/x-chm");
        contentTypeMap.put(".chrt" , "application/x-kchart");
        contentTypeMap.put(".class" , "application/x-java");
        contentTypeMap.put(".cls" , "text/x-tex");
        contentTypeMap.put(".cmake" , "text/x-cmake");
        contentTypeMap.put(".cpio" , "application/x-cpio");
        contentTypeMap.put(".cpio.gz" , "application/x-cpio-compressed");
        contentTypeMap.put(".cpp" , "text/x-c++src");
        contentTypeMap.put(".cr2" , "image/x-canon-cr2");
        contentTypeMap.put(".crt" , "application/x-x509-ca-cert");
        contentTypeMap.put(".crw" , "image/x-canon-crw");
        contentTypeMap.put(".cs" , "text/x-csharp");
        contentTypeMap.put(".csh" , "application/x-csh");
        contentTypeMap.put(".css" , "text/css");
        contentTypeMap.put(".cssl" , "text/css");
        contentTypeMap.put(".csv" , "text/csv");
        contentTypeMap.put(".cue" , "application/x-cue");
        contentTypeMap.put(".cur" , "image/x-win-bitmap");
        contentTypeMap.put(".cxx" , "text/x-c++src");
        contentTypeMap.put(".d" , "text/x-dsrc");
        contentTypeMap.put(".dar" , "application/x-dar");
        contentTypeMap.put(".dbf" , "application/x-dbf");
        contentTypeMap.put(".dc" , "application/x-dc-rom");
        contentTypeMap.put(".dcl" , "text/x-dcl");
        contentTypeMap.put(".dcm" , "application/dicom");
        contentTypeMap.put(".dcr" , "image/x-kodak-dcr");
        contentTypeMap.put(".dds" , "image/x-dds");
        contentTypeMap.put(".deb" , "application/x-deb");
        contentTypeMap.put(".der" , "application/x-x509-ca-cert");
        contentTypeMap.put(".desktop" , "application/x-desktop");
        contentTypeMap.put(".dia" , "application/x-dia-diagram");
        contentTypeMap.put(".diff" , "text/x-patch");
        contentTypeMap.put(".divx" , "video/x-msvideo");
        contentTypeMap.put(".djv" , "image/vnd.djvu");
        contentTypeMap.put(".djvu" , "image/vnd.djvu");
        contentTypeMap.put(".dng" , "image/x-adobe-dng");
        contentTypeMap.put(".doc" , "application/msword");
        contentTypeMap.put(".docbook" , "application/docbook+xml");
        contentTypeMap.put(".docm" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        contentTypeMap.put(".docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        contentTypeMap.put(".dot" , "text/vnd.graphviz");
        contentTypeMap.put(".dsl" , "text/x-dsl");
        contentTypeMap.put(".dtd" , "application/xml-dtd");
        contentTypeMap.put(".dtx" , "text/x-tex");
        contentTypeMap.put(".dv" , "video/dv");
        contentTypeMap.put(".dvi" , "application/x-dvi");
        contentTypeMap.put(".dvi.bz2" , "application/x-bzdvi");
        contentTypeMap.put(".dvi.gz" , "application/x-gzdvi");
        contentTypeMap.put(".dwg" , "image/vnd.dwg");
        contentTypeMap.put(".dxf" , "image/vnd.dxf");
        contentTypeMap.put(".e" , "text/x-eiffel");
        contentTypeMap.put(".egon" , "application/x-egon");
        contentTypeMap.put(".eif" , "text/x-eiffel");
        contentTypeMap.put(".el" , "text/x-emacs-lisp");
        contentTypeMap.put(".emf" , "image/x-emf");
        contentTypeMap.put(".emp" , "application/vnd.emusic-emusic_package");
        contentTypeMap.put(".ent" , "application/xml-external-parsed-entity");
        contentTypeMap.put(".eps" , "image/x-eps");
        contentTypeMap.put(".eps.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值