Java获取URL链接的⽂件类型
问题发⽣:
查看源码发现:
因为没有⽂件类型所以在转存图⽚的时候出错了
⾕歌百度了⼀下发现解决办法:
URLConnection class provides two static methods to help programs figure out the MIME type of some data; you can use these if the content type just isn't available or if you have reason to believe that the content type you're given isn't correct。
就是说URLConnection提供了两种⽅法可以猜测(根据实测结果,这个猜测是相当的准)数据的MIME
类型。
第⼀个是:
public static String guessContentTypeFromName(String name)
这个⽅法根据URL⽂件部分的后缀名来判断类型,跟之前我的⽅法⼀样。这个不能解决上⾯那个问题。
第⼆个是:
public static String guessContentTypeFromStream(InputStream in)
这个⽅法是根据流的前⾯⼏个字节来判断类型,这个就不需要⽂件后缀名了,完全可以解决上⾯那个问题。
BufferedInputStream bis = null;
HttpURLConnection urlconnection = null;
URL url = null;
url = new URL(strUrl);
urlconnection = (HttpURLConnection) url.openConnection();
bis = new InputStream());
System.out.println("file type:"+HttpURLConnection.guessContentTypeFromStream(bis));
但⽣产中发现有很多类型不能正确的判断出来,如bmp
然后继续各种,最后解决了...
再看⼀下⽆后缀名的判断..
HTTP Content-type 对照表
Content-Type,内容类型,⼀般是指⽹页中存在的Content-Type,⽤于定义⽹络⽂件的类型和⽹页的编码,决定浏览器将以什么形式、什么编码读取这个⽂件,⽐如⽤PHP输出图⽚⽂件、JSON数据、XML⽂件等⾮HTML内容时,就必须⽤header函数来指定Content-Type,才能达到输出⼀张图⽚或是其它指定内容类型的需求。
Application Type
⽂件扩
展名
Content-Type(Mime-Type)描述
.application/x-
.*application/octet-stream⼆进制流,不知道下载⽂件类型
.pdf application/pdf PDF(Portable Document Format的简称,意为“便携式⽂件格式”)
.
ai application/postscript PostScript(PS)是主要⽤于电⼦产业和桌⾯出版领域的⼀种页⾯描述语⾔和编程语⾔。
.xml application/atom+xml Atom feeds
.js application/ecmascript相当于application/javascript但是严格的处理规则
.edi application/EDI-X12EDI ANSI ASC X12数据
.edi application/EDIFACT EDI EDIFACT数据
.json application/json JSON(JavaScript Object Notation)
.js application/javascript ECMAScript/JavaScript(相当于application/ecmascript但是宽松的处理规则)
.ogg application/ogg Ogg, 视频⽂件格式
.rdf application/rdf+xml资源描述框架(Resource Description Framework,缩写 RDF),是万维⽹联盟(W3C)提出
的⼀组标记语⾔的技术标准,以便更为丰富地描述和表达⽹络资源的内容与结构。
.xml application/rss+xml RSS(Really Simple Syndication,简易信息聚合)是⼀种消息来源格式规范,⽤以聚合经常发布更新数据的⽹站,例如博客⽂章、新闻、⾳频或视频的⽹摘
.xml application/soap+xml简单对象访问协议(SOAP,全写为Simple Object Access Protocol)是交换数据的⼀种协议规
范,使⽤在计算机⽹络Web服务(web service)中,交换带结构信息。
.woff application/font-woff Web开放字体格式(Web Open Font Format,简称WOFF)是⼀种⽹页所采⽤的字体格式标准。(推荐使⽤;使⽤application/x-font-woff直到它变为官⽅标准)
.xhtml application/xhtml+xml可扩展超⽂本标记语⾔(eXtensible HyperText Markup Language,XHTML),是⼀种标记语
⾔,表现⽅式与超⽂本标记语⾔(HTML)类似,不过语法上更加严格。
.xml application/xml可扩展标记语⾔(英语:eXtensible Markup Language,简称: XML),是⼀种标记语⾔。
.dtd application/xml-dtd XML⽂件的⽂件型别定义(Document Type Definition)可以看成⼀个或者多个XML⽂件的模板,在这⾥可以定义XML⽂件中的元素、元素的属性、元素的排列⽅式、元素包含的内容等等。
.xml application/xop+xml⼆进制优化封装协议(Xmlbinary Optimized Packaging)
.zip application/zip ZIP压缩⽂件
.gzip application/gzip Gzip是若⼲种⽂件压缩程序的简称,通常指GNU计划的实现,此处的gzip代表GNU zip。.xls application/x-xls XLS 就是 Microsoft Excel ⼯作表,是⼀种⾮常常⽤的电⼦表格格式。
.001application/x-001
.301application/x-301
.906application/x-906
.a11application/x-a11
.awf application/vnd.adobe.workflow
.
bmp application/x-bmp
.c4t application/x-c4t
.cal application/x-cals
.cdf application/x-netcdf
.cel application/x-cel  .cg4application/x-g4 .cit application/x-cit  .bot application/x-bot
.c90
application/x-c90 .cat
application/vnd.ms-pki.seccat  .cdr
application/x-cdr  .cer
application/x-x509-ca-cert  .cgm
application/x-cgm  .cmx
application/x-cmx  .crl
application/pkix-crl  .csi
application/x-csi  .cut
application/x-cut  .dbm
application/x-dbm  .cmp
application/x-cmp  .cot
application/x-cot  .crt
application/x-x509-ca-cert  .dbf
application/x-dbf  .dbx
application/x-dbx  .dcx
application/x-dcx  .dgn
application/x-dgn  .dll
application/x-msdownload  .dot
application/msword  .der
application/x-x509-ca-cert  .dib
application/x-dib  .doc
application/msword  .drw
application/x-drw  .dwf
application/x-dwf  .dxb
application/x-dxb  .edn
application/vnd.adobe.edn  .dwg
application/x-dwg  .dxf
gzip是什么文件夹
application/x-dxf  .emf
application/x-emf  .epi
application/x-epi  .eps
application/postscript  .exe
application/x-msdownload  .fdf
application/vnd.fdf  .eps
application/x-ps  .etd
application/x-ebx  .fif
application/fractals  .frm
application/x-frm  .gbr
application/x-gbr  .g4
application/x-g4 .gl2
application/x-gl2 .hgl
application/x-hgl  .hpg
application/x-hpgl  .hqx
application/mac-binhex40 .hta
application/hta  .gp4
application/x-gp4 .hmr
application/x-hmr  .hpl
application/x-hpl  .hrf
application/x-hrf  .icb
application/x-icb  .ico
application/x-ico  .ig4
application/x-g4 .iii
application/x-iphone  .ins application/x-internet-signup  ⽂件扩展名Content-Type(Mime-Type)描述
.application/x-
.iff application/x-iff  .igs application/x-igs  .img application/x-img  .isp
application/x-internet-signup  .jpe
application/x-jpe  .js
application/x-javascript  .jpg
application/x-jpg  .lar
application/x-laplayer-reg  .latex
application/x-latex  .lbm
application/x-lbm  .ls
application/x-javascript  .ltr
application/x-ltr  .man
application/x-troff-man  .mdb
application/msaccess  .mac
application/x-mac  .mdb
application/x-mdb  .mfp
application/x-shockwave-flash  .mi
application/x-mi  .mil
application/x-mil  .mocha application/x-javascript  .mpd
application/vnd.ms-project  .mpp
application/vnd.ms-project  .mpt
application/vnd.ms-project  .mpw
application/vnd.ms-project  .mpx
application/vnd.ms-project  .mxp
application/x-mmxp  .nrf
application/x-nrf  .out
application/x-out  .p12
application/x-pkcs12 .p7c
application/pkcs7-mime  .p7r
application/x-pkcs7-certreqresp  .pc5
application/x-pc5 .pcl
application/x-pcl  .pdx
application/vnd.adobe.pdx  .pgl
application/x-pgl  .pko
application/vnd.ms-pki.pko  .p10
application/pkcs10 .p7b
application/x-pkcs7-certificates  .p7m
application/pkcs7-mime  .p7s
application/pkcs7-signature  .pci
application/x-pci  .pcx
application/x-pcx  .pdf
application/pdf  .pfx
application/x-pkcs12 .pic
application/x-pic  .pl
application/x-perl  .plt
application/x-plt  .png
application/x-png  .ppa
application/vnd.ms-powerpoint  .pps
application/vnd.ms-powerpoint  .ppt
application/x-ppt  .prf
application/pics-rules  .prt
application/x-prt  .ps
application/postscript  .pwz
application/vnd.ms-powerpoint  .ra
-realaudio RealAudio 是⼀种由RealNetworks 发展的RealMedia 多媒体⾳频⽂件格式,仅指RealPlayer 中能够识别的⾳频⽂件,也可以理解为real 格式的⾳频⽂件。.ras application/x-ras
⽂件扩展名Content-Type(Mime-Type)描述.application/x-
.pot application/vnd.ms-powerpoint  .ppm
application/x-ppm  .ppt application/vnd.ms-powerpoint  .pr
application/x-pr  .prn
application/x-prn  .ps
application/x-ps  .ptn
application/x-ptn  .red
application/x-red  .rjs
-realsystem-rjs  .rlc
application/x-rlc  .rm
-realmedia  .rat
application/rat-file  .rec
-recording  .rgb
application/x-rgb  .rjt
-realsystem-rjt  .rle
application/x-rle  .rmf
application/f  .rmj
-realsystem-rmj  .rmp
-rn_music_package  .rmvb
-realmedia-vbr  .rnx
-realplayer  .rpm
audio/x-pn-realaudio-plugin  .rms
-realmedia-secure  .rmx
-realsystem-rmx  .rsml
-rsml  .rtf
application/msword  .rv
-realvideo  .sat
application/x-sat  .sdw
application/x-sdw  .slb
application/x-slb  .rtf
application/x-rtf  .sam
application/x-sam  .sdp
application/sdp  .sit
application/x-stuffit  .sld
application/x-sld  .smi
application/smil  .smk
application/x-smk  .smil
application/smil  .spc
application/x-pkcs7-certificates  .spl
application/futuresplash  .ssm
application/streamingmedia  .stl
application/vnd.ms-pki.stl  .sst
application/store  .tdf
application/x-tdf  .tga
application/x-tga  .sty
application/x-sty  .swf
application/x-shockwave-flash  .tg4
application/x-tg4 .tif
application/x-tif  .vdx
application/vnd.visio  .vpg
application/x-vpeg005 .vsd
application/x-vsd  .vst application/vnd.visio  ⽂件扩展名Content-Type(Mime-Type)描述.application/x-

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。