這個接口專門爲用戶提供了不一樣的響應結果!(視頻,下載,圖片,excel,word等等功能)html
方法介紹:java
setContentType("text/xml"):設置響應類型緩存
如下是類型:app
超文本標記語言文本 .html text/htmlide
xml文檔 .xml text/xmlexcel
XHTML文檔 .xhtml application/xhtml+xmlcode
普通文本 .txt text/plain視頻
RTF文本 .rtf application/rtfxml
PDF文檔 .pdf application/pdfhtm
Microsoft Word文件 .word application/msword
PNG圖像 .png image/png
GIF圖形 .gif image/gif
JPEG圖形 .jpeg,.jpg image/jpeg
au聲音文件 .au audio/basic
MIDI音樂文件 mid,.midi audio/midi,audio/x-midi
RealAudio音樂文件 .ra, .ram audio/x-pn-realaudio
MPEG文件 .mpg,.mpeg video/mpeg
AVI文件 .avi video/x-msvideo
TAR文件 .tar application/x-tar
任意的二進制數據 application/octet-stream
getServletContext:容器上下文對象(理解成一個容器便可,裏面有獲取其餘文件信息的方)
是咧:InputStream in = getServletContext().getResourceAsStream("/res/小澤瑪利亞.avi");
如下是是咧:
這是servlet的get方法的寫法,頁面直接請求到此servlet並執行,就能得到這個文件了!
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //首先設置響應類型! response.setContentType("video/x-msvideo"); //輸入輸出流對象 InputStream in = null; OutputStream ou = null; //獲取文件到輸入流 in = getServletContext().getResourceAsStream("/res/01.avi"); ou = response.getOutputStream(); //設置一個輸出標誌 int i = -1; //緩存大小 byte[] b = new byte[1024]; while((i =in.read(b)) != -1){ ou.write(b, 0, i); } in.close(); ou.flush(); ou.close(); }
頁面請求:
<a href = "video.do">我要看片!!!</a>