問題發生:
Java從網絡批量讀取圖片並保存至本網站服務器后再插入文章中
今天轉入一篇文章 http://news.qq.com/a/20170605/045860.htm 發現圖片未能成功上傳
查看源碼發現:
因為沒有文件類型所以在轉存圖片的時候出錯了
谷歌百度了一下發現解決辦法:
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(); urlconnection.connect(); bis = new BufferedInputStream(urlconnection.getInputStream()); 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 | 二進制流,不知道下載文件類型 |
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-X12 | EDI 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 工作表,是一種非常常用的電子表格格式。 |
.001 | application/x-001 | |
.301 | application/x-301 | |
.906 | application/x-906 | |
.a11 | application/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 | |
.cg4 | application/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 | 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 | |
.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 | |
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 | audio/vnd.rn-realaudio | RealAudio是一種由RealNetworks發展的RealMedia多媒體音頻文件格式,僅指RealPlayer中能夠識別的音頻文件,也可以理解為real格式的音頻文件。 |
.ras | application/x-ras | |
.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 | application/vnd.rn-realsystem-rjs | |
.rlc | application/x-rlc | |
.rm | application/vnd.rn-realmedia | |
.rat | application/rat-file | |
.rec | application/vnd.rn-recording | |
.rgb | application/x-rgb | |
.rjt | application/vnd.rn-realsystem-rjt | |
.rle | application/x-rle | |
.rmf | application/vnd.adobe.rmf | |
.rmj | application/vnd.rn-realsystem-rmj | |
.rmp | application/vnd.rn-rn_music_package | |
.rmvb | application/vnd.rn-realmedia-vbr | |
.rnx | application/vnd.rn-realplayer | |
.rpm | audio/x-pn-realaudio-plugin | |
.rms | application/vnd.rn-realmedia-secure | |
.rmx | application/vnd.rn-realsystem-rmx | |
.rsml | application/vnd.rn-rsml | |
.rtf | application/msword | |
.rv | video/vnd.rn-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/vnd.ms-pki.certstore | |
.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 | |
.vsw | application/vnd.visio | |
.vtx | application/vnd.visio | |
.torrent | application/x-bittorrent | |
.vda | application/x-vda | |
.vsd | application/vnd.visio | |
.vss | application/vnd.visio | |
.vst | application/x-vst | |
.vsx | application/vnd.visio | |
.wb1 | application/x-wb1 | |
.wb3 | application/x-wb3 | |
.wiz | application/msword | |
.wk4 | application/x-wk4 | |
.wks | application/x-wks | |
.wb2 | application/x-wb2 | |
.wk3 | application/x-wk3 | |
.wkq | application/x-wkq | |
.wmf | application/x-wmf | |
.wmd | application/x-ms-wmd | |
.wp6 | application/x-wp6 | |
.wpg | application/x-wpg | |
.wq1 | application/x-wq1 | |
.wri | application/x-wri | |
.ws | application/x-ws | |
.wmz | application/x-ms-wmz | |
.wpd | application/x-wpd | |
.wpl | application/vnd.ms-wpl | |
.wr1 | application/x-wr1 | |
.wrk | application/x-wrk | |
.ws2 | application/x-ws | |
.xdp | application/vnd.adobe.xdp | |
.xfd | application/vnd.adobe.xfd | |
.xfdf | application/vnd.adobe.xfdf | |
.xls | application/vnd.ms-excel | |
.xwd | application/x-xwd | |
.sis | application/vnd.symbian.install | |
.x_t | application/x-x_t | |
.apk | application/vnd.android.package-archive | |
.x_b | application/x-x_b | |
.sisx | application/vnd.symbian.install | |
.ipa | application/vnd.iphone | |
.xap | application/x-silverlight-app | |
.xlw | application/x-xlw | |
.xpl | audio/scpls | |
.anv | application/x-anv | |
.uin | application/x-icq |
Text Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.323 | text/h323 | |
.biz | text/xml | |
.cml | text/xml | |
.asa | text/asa | |
.asp | text/asp | |
.css | text/css | 層疊樣式表(英語:Cascading Style Sheets,簡寫CSS),又稱串樣式列表、層次結構式樣式表文件,一種用來為結構化文檔(如HTML文檔或XML應用)添加樣式(字體、間距和顏色等)的計算機語言,由W3C定義和維護。 |
.csv | text/csv | 逗號分隔值(Comma-Separated Values,CSV,有時也稱為字符分隔值,因為分隔字符也可以不是逗號),其文件以純文本形式存儲表格數據(數字和文本)。 |
.dcd | text/xml | |
.dtd | text/xml | |
.ent | text/xml | |
.fo | text/xml | |
.htc | text/x-component | |
.html | text/html | |
.htx | text/html | |
.htm | text/html | |
.htt | text/webviewhtml | |
.jsp | text/html | |
.math | text/xml | |
.mml | text/xml | |
.mtx | text/xml | |
.plg | text/html | |
.rdf | text/xml | |
.rt | text/vnd.rn-realtext | |
.sol | text/plain | |
.spp | text/xml | |
.stm | text/html | |
.svg | text/xml | |
.tld | text/xml | |
.txt | text/plain | 純文字內容 |
.uls | text/iuls | |
.vml | text/xml | |
.tsd | text/xml | |
.vcf | text/x-vcard | |
.vxml | text/xml | |
.wml | text/vnd.wap.wml | |
.wsdl | text/xml | |
.wsc | text/scriptlet | |
.xdr | text/xml | |
.xql | text/xml | |
.xsd | text/xml | |
.xslt | text/xml | |
.xml | text/xml | |
.xq | text/xml | |
.xquery | text/xml | |
.xsl | text/xml | |
.xhtml | text/html | |
.odc | text/x-ms-odc | |
.r3t | text/vnd.rn-realtext3d | |
.sor | text/plain |
Audio Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.acp | audio/x-mei-aac | |
.aif | audio/aiff | |
.aiff | audio/aiff | |
.aifc | audio/aiff | |
.au | audio/basic | |
.la1 | audio/x-liquid-file | |
.lavs | audio/x-liquid-secure | |
.lmsff | audio/x-la-lms | |
.m3u | audio/mpegurl | |
.midi | audio/mid | |
.mid | audio/mid | |
.mp2 | audio/mp2 | |
.mp3 | audio/mp3 | |
.mp4 | audio/mp4 | MP4,全稱MPEG-4 Part 14,是一種使用MPEG-4的多媒體計算機文件格式,擴展名為.mp4,以存儲數字音頻及數字視頻為主。 |
.mnd | audio/x-musicnet-download | |
.mp1 | audio/mp1 | |
.mns | audio/x-musicnet-stream | |
.mpga | audio/rn-mpeg | |
.pls | audio/scpls | |
.ram | audio/x-pn-realaudio | |
.rmi | audio/mid | |
.rmm | audio/x-pn-realaudio | |
.snd | audio/basic | |
.wav | audio/wav | |
.wax | audio/x-ms-wax | |
.wma | audio/x-ms-wma |
Video Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.asf | video/x-ms-asf | |
.asx | video/x-ms-asf | |
.avi | video/avi | |
.IVF | video/x-ivf | |
.m1v | video/x-mpeg | |
.m2v | video/x-mpeg | |
.m4e | video/mpeg4 | |
.movie | video/x-sgi-movie | |
.mp2v | video/mpeg | |
.mp4 | video/mpeg4 | MP4,全稱MPEG-4 Part 14,是一種使用MPEG-4的多媒體計算機文件格式,擴展名為.mp4,以存儲數字音頻及數字視頻為主。 |
.mpa | video/x-mpg | |
.mpe | video/x-mpeg | |
.mpg | video/mpg | |
.mpeg | video/mpg | |
.mps | video/x-mpeg | |
.mpv | video/mpg | |
.mpv2 | video/mpeg | |
.wm | video/x-ms-wm | |
.wmv | video/x-ms-wmv | |
.wmx | video/x-ms-wmx | |
.wvx | video/x-ms-wvx |
Image Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.tif | image/tiff | 標簽圖像文件格式(Tagged Image File Format,簡寫為TIFF)是一種主要用來存儲包括照片和藝術圖在內的圖像的文件格式。它最初由Aldus公司與微軟公司一起為PostScript打印開發。 |
.fax | image/fax | |
.gif | image/gif | 圖像互換格式(GIF,Graphics Interchange Format)是一種位圖圖形文件格式,以8位色(即256種顏色)重現真彩色的圖像。 |
.ico | image/x-icon | |
.jfif | image/jpeg | |
.jpe | image/jpeg | |
.jpeg | image/jpeg | JPEG是一種針對相片圖像而廣泛使用的一種有損壓縮標准方法。 |
.jpg | image/jpeg | |
.net | image/pnetvue | |
.png | image/png | 便攜式網絡圖形(Portable Network Graphics,PNG)是一種無損壓縮的位圖圖形格式,支持索引、灰度、RGB三種顏色方案以及Alpha通道等特性。 |
.rp | image/vnd.rn-realpix | |
.tif | image/tiff | |
.tiff | image/tiff | |
.wbmp | image/vnd.wap.wbmp |
Message Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.eml | message/rfc822 | |
.mht | message/rfc822 | |
.mhtml | message/rfc822 | |
.nws | message/rfc822 |
Drawing Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.907 | drawing/907 | |
.slk | drawing/x-slk | |
.top | drawing/x-top |
Java Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.class | java/* | |
.java | java/* |
Other Type
文件擴展名 | Content-Type(Mime-Type) | 描述 |
---|---|---|
.dwf | Model/vnd.dwf |