文件下载及web文件的contentType类型大全
String filePath=""; File file=new File(filePath); //解决乱码问题 String filename=URLEncoder.encode(file.getName(),"utf-8"); //重置输出流 response.reset(); Response.AddHeader("Content-Disposition", "attachment;filename="+filename);//设置文件名 response.addHeader("Content-Length",file.length);//设置下载文件大小 response.setContentType("application/octet-stream");//设置文件类型 OutputStream toClient=new BufferedOuntputStream( response.getOutputStream() );//获取二进制输出流 //读取文件数据 InputStream fis=new BufferedInputStream(new FileInputStream(filePath)); byte[] buffer=new byte[file.length()]; fis.read(buffer); fis.close(); //输出文件数据 toClient.write(buffer); toClient.close(); 文件类型如下: ".*"="application/octet-stream" ".001"="application/x-001" ".301"="application/x-301" ".323"="text/h323" ".906"="application/x-906" ".907"="drawing/907" ".a11"="application/x-a11" ".acp"="audio/x-mei-aac" ".ai"="application/postscript" ".aif"="audio/aiff" ".aifc"="audio/aiff" ".aiff"="audio/aiff" ".anv"="application/x-anv" ".asa"="text/asa" ".asf"="video/x-ms-asf" ".asp"="text/asp" ".asx"="video/x-ms-asf" ".au"="audio/basic" ".avi"="video/avi" ".awf"="application/vnd.adobe.workflow" ".biz"="text/xml" ".bmp"="application/x-bmp" ".bot"="application/x-bot" ".c4t"="application/x-c4t" ".c90"="application/x-c90" ".cal"="application/x-cals" ".cat"="application/vnd.ms-pki.seccat" ".cdf"="application/x-netcdf" ".cdr"="application/x-cdr" ".cel"="application/x-cel" ".cer"="application/x-x509-ca-cert" ".cg4"="application/x-g4" ".cgm"="application/x-cgm" ".cit"="application/x-cit" ".class"="java/*" ".cml"="text/xml" ".cmp"="application/x-cmp" ".cmx"="application/x-cmx" ".cot"="application/x-cot" ".crl"="application/pkix-crl" ".crt"="application/x-x509-ca-cert" ".csi"="application/x-csi" ".css"="text/css" ".cut"="application/x-cut" ".dbf"="application/x-dbf" ".dbm"="application/x-dbm" ".dbx"="application/x-dbx" ".dcd"="text/xml" ".dcx"="application/x-dcx" ".der"="application/x-x509-ca-cert" ".dgn"="application/x-dgn" ".dib"="application/x-dib" ".dll"="application/x-msdownload" ".doc"="application/msword" ".dot"="application/msword" ".drw"="application/x-drw" ".dtd"="text/xml" ".dwf"="Model/vnd.dwf" ".dwf"="application/x-dwf" ".dwg"="application/x-dwg" ".dxb"="application/x-dxb" ".dxf"="application/x-dxf" ".edn"="application/vnd.adobe.edn" ".emf"="application/x-emf" ".eml"="message/rfc822" ".ent"="text/xml" ".epi"="application/x-epi" ".eps"="application/x-ps" ".eps"="application/postscript" ".etd"="application/x-ebx" ".exe"="application/x-msdownload" ".fax"="image/fax" ".fdf"="application/vnd.fdf" ".fif"="application/fractals" ".fo"="text/xml" ".frm"="application/x-frm" ".g4"="application/x-g4" ".gbr"="application/x-gbr" ".gcd"="application/x-gcd" ".gif"="image/gif" ".gl2"="application/x-gl2" ".gp4"="application/x-gp4" ".hgl"="application/x-hgl" ".hmr"="application/x-hmr" ".hpg"="application/x-hpgl" ".hpl"="application/x-hpl" ".hqx"="application/mac-binhex40" ".hrf"="application/x-hrf" ".hta"="application/hta" ".htc"="text/x-component" ".htm"="text/html" ".html"="text/html" ".htt"="text/webviewhtml" ".htx"="text/html" ".icb"="application/x-icb" ".ico"="image/x-icon" ".ico"="application/x-ico" ".iff"="application/x-iff" ".ig4"="application/x-g4" ".igs"="application/x-igs" ".iii"="application/x-iphone" ".img"="application/x-img" ".ins"="application/x-internet-signup" ".isp"="application/x-internet-signup" ".IVF"="video/x-ivf" ".java"="java/*" ".jfif"="image/jpeg" ".jpe"="image/jpeg" ".jpe"="application/x-jpe" ".jpeg"="image/jpeg" ".jpg"="image/jpeg" ".jpg"="application/x-jpg" ".js"="application/x-javascript" ".jsp"="text/html" ".la1"="audio/x-liquid-file" ".lar"="application/x-laplayer-reg" ".latex"="application/x-latex" ".lavs"="audio/x-liquid-secure" ".lbm"="application/x-lbm" ".lmsff"="audio/x-la-lms" ".ls"="application/x-javascript" ".ltr"="application/x-ltr" ".m1v"="video/x-mpeg" ".m2v"="video/x-mpeg" ".m3u"="audio/mpegurl" ".m4e"="video/mpeg4" ".mac"="application/x-mac" ".man"="application/x-troff-man" ".math"="text/xml" ".mdb"="application/msaccess" ".mdb"="application/x-mdb" ".mfp"="application/x-shockwave-flash" ".mht"="message/rfc822" ".mhtml"="message/rfc822" ".mi"="application/x-mi" ".mid"="audio/mid" ".midi"="audio/mid" ".mil"="application/x-mil" ".mml"="text/xml" ".mnd"="audio/x-musicnet-download" ".mns"="audio/x-musicnet-stream" ".mocha"="application/x-javascript" ".movie"="video/x-sgi-movie" ".mp1"="audio/mp1" ".mp2"="audio/mp2" ".mp2v"="video/mpeg" ".mp3"="audio/mp3" ".mp4"="video/mpeg4" ".mpa"="video/x-mpg" ".mpd"="application/vnd.ms-project" ".mpe"="video/x-mpeg" ".mpeg"="video/mpg" ".mpg"="video/mpg" ".mpga"="audio/rn-mpeg" ".mpp"="application/vnd.ms-project" ".mps"="video/x-mpeg" ".mpt"="application/vnd.ms-project" ".mpv"="video/mpg" ".mpv2"="video/mpeg" ".mpw"="application/vnd.ms-project" ".mpx"="application/vnd.ms-project" ".mtx"="text/xml" ".mxp"="application/x-mmxp" ".net"="image/pnetvue" ".nrf"="application/x-nrf" ".nws"="message/rfc822" ".odc"="text/x-ms-odc" ".out"="application/x-out" ".p10"="application/pkcs10" ".p12"="application/x-pkcs12" ".p7b"="application/x-pkcs7-certificates" ".p7c"="application/pkcs7-mime" ".p7m"="application/pkcs7-mime" ".p7r"="application/x-pkcs7-certreqresp" ".p7s"="application/pkcs7-signature" ".pc5"="application/x-pc5" ".pci"="application/x-pci" ".pcl"="application/x-pcl" ".pcx"="application/x-pcx" ".pdf"="application/pdf" ".pdf"="application/pdf" ".pdx"="application/vnd.adobe.pdx" ".pfx"="application/x-pkcs12" ".pgl"="application/x-pgl" ".pic"="application/x-pic" ".pko"="application/vnd.ms-pki.pko" ".pl"="application/x-perl" ".plg"="text/html" ".pls"="audio/scpls" ".plt"="application/x-plt" ".png"="image/png" ".png"="application/x-png" ".pot"="application/vnd.ms-powerpoint" ".ppa"="application/vnd.ms-powerpoint" ".ppm"="application/x-ppm" ".pps"="application/vnd.ms-powerpoint" ".ppt"="application/vnd.ms-powerpoint" ".ppt"="application/x-ppt" ".pr"="application/x-pr" ".prf"="application/pics-rules" ".prn"="application/x-prn" ".prt"="application/x-prt" ".ps"="application/x-ps" ".ps"="application/postscript" ".ptn"="application/x-ptn" ".pwz"="application/vnd.ms-powerpoint" ".r3t"="text/vnd.rn-realtext3d" ".ra"="audio/vnd.rn-realaudio" ".ram"="audio/x-pn-realaudio" ".ras"="application/x-ras" ".rat"="application/rat-file" ".rdf"="text/xml" ".rec"="application/vnd.rn-recording" ".red"="application/x-red" ".rgb"="application/x-rgb" ".rjs"="application/vnd.rn-realsystem-rjs" ".rjt"="application/vnd.rn-realsystem-rjt" ".rlc"="application/x-rlc" ".rle"="application/x-rle" ".rm"="application/vnd.rn-realmedia" ".rmf"="application/vnd.adobe.rmf" ".rmi"="audio/mid" ".rmj"="application/vnd.rn-realsystem-rmj" ".rmm"="audio/x-pn-realaudio" ".rmp"="application/vnd.rn-rn_music_package" ".rms"="application/vnd.rn-realmedia-secure" ".rmvb"="application/vnd.rn-realmedia-vbr" ".rmx"="application/vnd.rn-realsystem-rmx" ".rnx"="application/vnd.rn-realplayer" ".rp"="image/vnd.rn-realpix" ".rpm"="audio/x-pn-realaudio-plugin" ".rsml"="application/vnd.rn-rsml" ".rt"="text/vnd.rn-realtext" ".rtf"="application/msword" ".rtf"="application/x-rtf" ".rv"="video/vnd.rn-realvideo" ".sam"="application/x-sam" ".sat"="application/x-sat" ".sdp"="application/sdp" ".sdw"="application/x-sdw" ".sit"="application/x-stuffit" ".slb"="application/x-slb" ".sld"="application/x-sld" ".slk"="drawing/x-slk" ".smi"="application/smil" ".smil"="application/smil" ".smk"="application/x-smk" ".snd"="audio/basic" ".sol"="text/plain" ".sor"="text/plain" ".spc"="application/x-pkcs7-certificates" ".spl"="application/futuresplash" ".spp"="text/xml" ".ssm"="application/streamingmedia" ".sst"="application/vnd.ms-pki.certstore" ".stl"="application/vnd.ms-pki.stl" ".stm"="text/html" ".sty"="application/x-sty" ".svg"="text/xml" ".swf"="application/x-shockwave-flash" ".tdf"="application/x-tdf" ".tg4"="application/x-tg4" ".tga"="application/x-tga" ".tif"="image/tiff" ".tif"="application/x-tif" ".tiff"="image/tiff" ".tld"="text/xml" ".top"="drawing/x-top" ".torrent"="application/x-bittorrent" ".tsd"="text/xml" ".txt"="text/plain" ".uin"="application/x-icq" ".uls"="text/iuls" ".vcf"="text/x-vcard" ".vda"="application/x-vda" ".vdx"="application/vnd.visio" ".vml"="text/xml" ".vpg"="application/x-vpeg005" ".vsd"="application/vnd.visio" ".vsd"="application/x-vsd" ".vss"="application/vnd.visio" ".vst"="application/vnd.visio" ".vst"="application/x-vst" ".vsw"="application/vnd.visio" ".vsx"="application/vnd.visio" ".vtx"="application/vnd.visio" ".vxml"="text/xml" ".wav"="audio/wav" ".wax"="audio/x-ms-wax" ".wb1"="application/x-wb1" ".wb2"="application/x-wb2" ".wb3"="application/x-wb3" ".wbmp"="image/vnd.wap.wbmp" ".wiz"="application/msword" ".wk3"="application/x-wk3" ".wk4"="application/x-wk4" ".wkq"="application/x-wkq" ".wks"="application/x-wks" ".wm"="video/x-ms-wm" ".wma"="audio/x-ms-wma" ".wmd"="application/x-ms-wmd" ".wmf"="application/x-wmf" ".wml"="text/vnd.wap.wml" ".wmv"="video/x-ms-wmv" ".wmx"="video/x-ms-wmx" ".wmz"="application/x-ms-wmz" ".wp6"="application/x-wp6" ".wpd"="application/x-wpd" ".wpg"="application/x-wpg" ".wpl"="application/vnd.ms-wpl" ".wq1"="application/x-wq1" ".wr1"="application/x-wr1" ".wri"="application/x-wri" ".wrk"="application/x-wrk" ".ws"="application/x-ws" ".ws2"="application/x-ws" ".wsc"="text/scriptlet" ".wsdl"="text/xml" ".wvx"="video/x-ms-wvx" ".xdp"="application/vnd.adobe.xdp" ".xdr"="text/xml" ".xfd"="application/vnd.adobe.xfd" ".xfdf"="application/vnd.adobe.xfdf" ".xhtml"="text/html" ".xls"="application/vnd.ms-excel" ".xls"="application/x-xls" ".xlw"="application/x-xlw" ".xml"="text/xml" ".xpl"="audio/scpls" ".xq"="text/xml" ".xql"="text/xml" ".xquery"="text/xml" ".xsd"="text/xml" ".xsl"="text/xml" ".xslt"="text/xml" ".xwd"="application/x-xwd" ".x_b"="application/x-x_b" ".x_t"="application/x-x_t" ### 文件下载及Web文件的ContentType类型详解 在Web开发中,处理文件下载是常见的需求之一。为了确保用户能够正确地下载文件并且浏览器能够正确解析文件类型,开发者需要正确设置HTTP响应头中的`Content-Type`字段。本文将详细介绍如何实现文件下载功能以及不同文件类型的`Content-Type`设置。 #### 一、文件下载的基本实现 文件下载的核心在于通过HTTP响应将文件内容发送给客户端,并且通过设置特定的HTTP头部来告诉浏览器应该如何处理这些数据。以下是一个Java示例代码,展示如何设置HTTP响应头并传输文件: ```java String filePath = ""; // 文件路径 File file = new File(filePath); // 解决文件名中的乱码问题 String filename = URLEncoder.encode(file.getName(), "UTF-8"); // 重置输出流 response.reset(); // 设置文件下载方式(attachment表示附件下载) response.setHeader("Content-Disposition", "attachment; filename=" + filename); // 设置文件大小 response.addHeader("Content-Length", String.valueOf(file.length())); // 设置文件类型 response.setContentType("application/octet-stream"); // 获取输出流 OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); // 读取文件数据 InputStream fis = new BufferedInputStream(new FileInputStream(filePath)); byte[] buffer = new byte[(int) file.length()]; fis.read(buffer); fis.close(); // 输出文件数据到客户端 toClient.write(buffer); toClient.close(); ``` #### 二、常见文件类型的ContentType设置 `Content-Type`字段用于指定MIME类型,它定义了数据格式,以便浏览器或其他接收端知道如何正确解析接收到的数据。下面是部分常见文件类型的MIME类型设置: 1. **图像文件** - `.gif` = `image/gif` - `.jpg` = `image/jpeg` - `.png` = `image/png` - `.bmp` = `application/x-bmp` 2. **音频文件** - `.mp3` = `audio/mp3` - `.wav` = `audio/wav` - `.aif` = `audio/aiff` - `.au` = `audio/basic` 3. **视频文件** - `.avi` = `video/avi` - `.mp4` = `video/mpeg4` - `.wmv` = `video/x-ms-wmv` - `.asf` = `video/x-ms-asf` 4. **文档文件** - `.pdf` = `application/pdf` - `.doc` = `application/msword` - `.xls` = `application/vnd.ms-excel` - `.ppt` = `application/vnd.ms-powerpoint` 5. **Web文件** - `.html` = `text/html` - `.xml` = `text/xml` - `.css` = `text/css` - `.js` = `application/x-javascript` 6. **其他文件** - `.zip` = `application/zip` - `.rar` = `application/x-rar-compressed` - `.exe` = `application/x-msdownload` - `.dll` = `application/x-msdownload` 7. **多媒体文件** - `.mpg` = `video/mpg` - `.mpeg` = `video/mpg` - `.mpe` = `video/x-mpeg` - `.mp2v` = `video/mpeg` 8. **特殊用途文件** - `.torrent` = `application/x-bittorrent` - `.eml` = `message/rfc822` - `.msg` = `application/vnd.ms-outlook` 9. **编程与脚本文件** - `.java` = `java/*` - `.jsp` = `text/html` - `.php` = `text/html` - `.pl` = `application/x-perl` 10. **字体与图标文件** - `.ico` = `image/x-icon` - `.ttf` = `font/truetype` - `.woff` = `font/woff` - `.woff2` = `font/woff2` 11. **办公软件文件** - `.docx` = `application/vnd.openxmlformats-officedocument.wordprocessingml.document` - `.xlsx` = `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` - `.pptx` = `application/vnd.openxmlformats-officedocument.presentationml.presentation` 12. **特殊文件类型** - `.*` = `application/octet-stream` - `.001` = `application/x-001` - `.301` = `application/x-301` - `.323` = `text/h323` - `.906` = `application/x-906` - `.907` = `drawing/907` - `.a11` = `application/x-a11` - `.acp` = `audio/x-mei-aac` - `.ai` = `application/postscript` - `.aif` = `audio/aiff` - `.aifc` = `audio/aiff` - `.aiff` = `audio/aiff` - `.anv` = `application/x-anv` - `.asa` = `text/asa` - `.asf` = `video/x-ms-asf` - `.asp` = `text/asp` - `.asx` = `video/x-ms-asf` - `.au` = `audio/basic` - `.avi` = `video/avi` - `.awf` = `application/vnd.adobe.workflow` - `.biz` = `text/xml` - `.bmp` = `application/x-bmp` - `.bot` = `application/x-bot` - `.c4t` = `application/x-c4t` - `.c90` = `application/x-c90` - `.cal` = `application/x-cals` - `.cat` = `application/vnd.ms-pki.seccat` - `.cdf` = `application/x-netcdf` - `.cdr` = `application/x-cdr` - `.cel` = `application/x-cel` - `.cer` = `application/x-x509-ca-cert` - `.cg4` = `application/x-g4` - `.cgm` = `application/x-cgm` - `.cit` = `application/x-cit` - `.class` = `java/*` - `.cml` = `text/xml` - `.cmp` = `application/x-cmp` - `.cmx` = `application/x-cmx` - `.cot` = `application/x-cot` - `.crl` = `application/pkix-crl` - `.crt` = `application/x-x509-ca-cert` - `.csi` = `application/x-csi` - `.css` = `text/css` - `.cut` = `application/x-cut` - `.dbf` = `application/x-dbf` - `.dbm` = `application/x-dbm` - `.dbx` = `application/x-dbx` - `.dcd` = `text/xml` - `.dcx` = `application/x-dcx` - `.der` = `application/x-x509-ca-cert` - `.dgn` = `application/x-dgn` - `.dib` = `application/x-dib` - `.dll` = `application/x-msdownload` - `.doc` = `application/msword` - `.dot` = `application/msword` #### 三、总结 通过以上内容,我们可以了解到不同文件类型的`Content-Type`设置对于文件下载来说至关重要。正确的设置不仅能够提升用户体验,还能避免因浏览器解析错误导致的问题。在实际应用中,开发者应根据具体需求选择合适的`Content-Type`值。此外,还需注意编码问题,例如中文文件名的乱码问题,这同样会影响用户的下载体验。
- 水_2014-03-29类型蛮全的,不过有些还是没有,总体还不错
- kent__liu2015-06-27不错,可以用。反而国外网站下载的,就不能用
- 粉丝: 0
- 资源: 1
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- (源码)基于ESP8266的WebDAV服务器与3D打印机管理系统.zip
- (源码)基于Nio实现的Mycat 2.0数据库代理系统.zip
- (源码)基于Java的高校学生就业管理系统.zip
- (源码)基于Spring Boot框架的博客系统.zip
- (源码)基于Spring Boot框架的博客管理系统.zip
- (源码)基于ESP8266和Blynk的IR设备控制系统.zip
- (源码)基于Java和JSP的校园论坛系统.zip
- (源码)基于ROS Kinetic框架的AGV激光雷达导航与SLAM系统.zip
- (源码)基于PythonDjango框架的资产管理系统.zip
- (源码)基于计算机系统原理与Arduino技术的学习平台.zip