這幾天一直很納悶,在Tomcat部署的網站中的下載下傳檔案中,如果檔案是rar類型的,一點選下載下傳rar檔案就直接打開,并且出現亂碼,右鍵另存為浏覽器也是預設為html格式,一直以為是浏覽器IE的問題,後來發現其實不關浏覽器的問題,而是Tomcat伺服器配置的問題,具體如下:
web.xml檔案中配置<mime-mapping>下載下傳檔案類型
TOMCAT在預設情況下下載下傳.rar的檔案是把檔案當作text打開,以至于IE打開RAR檔案為亂碼,如果遇到這種情況時不必認為是浏覽器的問題,大多數浏覽器應該不會死皮賴臉地把二進制檔案當作文本打開,一般都是伺服器給什麼浏覽器就開什麼.解決方法:
打開conf/web.xml,加入下面的代碼.

<mime-mapping>

<extension>doc</extension>

<mime-type>application/msword</mime-type>

</mime-mapping>


<extension>xls</extension>

<mime-type>application/msexcel</mime-type>



<extension>pdf</extension>

<mime-type>application/pdf</mime-type>

</mime-mapping>


<extension>zip</extension>

<mime-type>application/zip</mime-type>



<extension>rar</extension>

<mime-type>application/rar</mime-type>



<extension>txt</extension>

<mime-type>application/txt</mime-type>



<extension>chm</extension>

<mime-type>application/mshelp</mime-type>


<mime-mapping>

<extension>mp3</extension>

<mime-type>audio/x-mpeg</mime-type>


重新開機TOMCAT,清除IE緩存,再打開RAR的檔案時就可以正常下載下傳了.
不同的ContentType 會影響用戶端所看到的效果.
預設的ContentType為 text/html 也就是網頁格式.
代碼如:

<% response.ContentType ="text/html" %>

<!--#i nclude virtual="/ContentType.html" -->
顯示的為網頁,而

<% response.ContentType ="text/plain" %>

<!--#i nclude virtual="/sscript/ContentType.html" -->
則會顯示html原代碼.
以下為一些常用的 ContentType

GIF images

<% response.ContentType ="image/gif" %>

<!--#i nclude virtual="/myimage.gif" -->

JPEG images

<% response.ContentType ="image/jpeg" %>

<!--#i nclude virtual="/myimage.jpeg" -->

TIFF images

<% response.ContentType ="image/tiff" %>

<!--#i nclude virtual="/myimage.tiff" -->

MICROSOFT WORD document

<% response.ContentType ="application/msword" %>

<!--#i nclude virtual="/myfile.doc" -->

RTF document

<% response.ContentType ="application/rtf" %>

<!--#i nclude virtual="/myfile.rtf" -->

MICROSOFT EXCEL document

<% response.ContentType ="application/x-excel" %>

<!--#i nclude virtual="/myfile.xls" -->

MICROSOFT POWERPOINT document

<% response.ContentType ="application/ms-powerpoint" %>

<!--#i nclude virtual="/myfile.pff" -->

PDF document

<% response.ContentType ="application/pdf" %>

<!--#i nclude virtual="/myfile.pdf" -->

ZIP document

<% response.ContentType ="application/zip" %>

<!--#i nclude virtual="/myfile.zip" -->

下面是更詳細的ContentType

application/andrew-inset ez

application/mac-binhex40 hqx

application/mac-compactpro cpt

application/mathml+xml mathml

application/msword doc

application/octet-stream bin dms lha lzh exe class so dll

application/oda oda

application/ogg ogg

application/pdf pdf

application/postscript ai eps ps

application/rdf+xml rdf

application/smil smi smil

application/srgs gram

application/srgs+xml grxml

application/vnd.mif mif

application/vnd.mozilla.xul+xml xul

application/vnd.ms-excel xls

application/vnd.ms-powerpoint ppt

application/vnd.wap.wbxml wbxml

application/vnd.wap.wmlc .wmlc wmlc

application/vnd.wap.wmlscriptc .wmlsc wmlsc

application/voicexml+xml vxml

application/x-bcpio bcpio

application/x-cdlink vcd

application/x-chess-pgn pgn

application/x-cpio cpio