自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

介紹JSP HTTP服務(wù)器實(shí)現(xiàn)的以下特性

開(kāi)發(fā) 后端
本文介紹JSP HTTP服務(wù)器實(shí)現(xiàn)的以下特性,包括文件流請(qǐng)求的處理示例代碼和二進(jìn)制流文件的處理示例代碼。

JSP HTTP服務(wù)器對(duì)常規(guī)請(qǐng)求的支持

這里的常規(guī)請(qǐng)求是指請(qǐng)求的資源為文件類型,不需要進(jìn)行解釋,編譯,執(zhí)行等處理。例如:文本文件(*.TXT),超文本文件(*.HTM,*.HTML),腳本文件(*.JS,*.VBS等),圖片文件(*.JPG,*.PNG,*.GIF,*.BMP)。

處理基于文件流的請(qǐng)求較為簡(jiǎn)單。只需要讀取本地的文件資源,再發(fā)送給客戶端即可。

1.JSP HTTP服務(wù)器文件流請(qǐng)求的處理示例代碼

  1. //Create client socket output stream   
  2. m_sout = new PrintWriter(m_s.getOutputStream(), true);  
  3. m_soutx = null;  
  4. m_sout.println("HTTP/1.0 200 OK\nMIME-Version:1.0\nContent-Type:text/html\n\n");  
  5. File file = new File(fileName);  
  6. if(file.exists() == true)  
  7. {  
  8. //Create local file input stream   
  9. BufferedReader fin = new BufferedReader(new FileReader(file) );  
  10. String line = null;  
  11. String response = "";  
  12. //Read file by lines   
  13. while( (line = fin.readLine() ) != null)  
  14. {  
  15. responseresponse = response + line + "\n";  
  16. }  
  17. //Send the content to client socket   
  18. m_sout.println(response);  
  19. //Close local file handle   
  20. fin.close();  
  21. }  

以上是處理基于文本流的請(qǐng)求,以下是處理基于二進(jìn)制流的請(qǐng)求實(shí)例代碼。

2.JSP HTTP服務(wù)器二進(jìn)制流文件的處理示例代碼

  1. //Create client socket output stream   
  2. m_sm_soutx = m_s.getOutputStream();  
  3. m_sout = null;  
  4. String header = "HTTP/1.0 200 OK\nMIME-Version:1.0\n";  
  5. //Send content to client socket   
  6. m_soutx.write(header.getBytes() );  
  7. String mime = "";  
  8. //Get MIME by file type   
  9. switch(typeFlag)  
  10. {  
  11. case TYPE_JPEG: //jpeg file   
  12. {  
  13. mime = "image/jpeg";  
  14. break;  
  15. }  
  16. case TYPE_GIF: //gif file   
  17. {  
  18. mime = "image/gif";  
  19. break;  
  20. }  
  21. case TYPE_BMP: //bmp file   
  22. {  
  23. mime = "image/bmp";  
  24. break;  
  25. }  
  26. case TYPE_PNG: //png file   
  27. {  
  28. mime = "image/png";  
  29. break;  
  30. }  
  31. }  
  32. mime = "Content-Type:" + mime + "\n\n";  
  33. m_soutx.write(mime.getBytes() );  
  34. File file = new File(fileName);  
  35. if(file.exists() == true) //Read image files and send to client socket   
  36. {  
  37. //Create local file input stream   
  38. RandomAccessFile fin = new RandomAccessFile(fileName, "r");  
  39. final long size = fin.length();  
  40. byte [] buffer = new byte[(int)size];  
  41. fin.readFully(buffer);  
  42. fin.close();  
  43. //Send data to client socket   
  44. m_soutx.write(buffer);  
  45. }  
  46. //Close client socket output stream   
  47. m_soutx.close();  

從以上代碼可以看出,處理文本流和二進(jìn)制流的請(qǐng)求的方式是不相同的,文本流的文件是按照行進(jìn)行處理,而二進(jìn)制流的文件是以批量讀取。

其中關(guān)鍵的是,對(duì)于不同的文件類型,發(fā)送數(shù)據(jù)給客戶端時(shí)必須指明服務(wù)器端應(yīng)答的媒體類型,即MIME(Multipurpose Internet Mail Extensions),這樣應(yīng)答給客戶端的資源才能被客戶端瀏覽器所識(shí)別,并調(diào)用相應(yīng)的應(yīng)用程序?qū)Y源進(jìn)行讀取。
文件類型 擴(kuò)展名 MIME
文本文件 .TXT text/plain
HTML(HyperText Markup Language)文件 .HTML,.HTM text/html
JPEG(Joint Photographic Experts Group)文件 .JPG,.JPEG image/jpeg
PNG(Portable Network Graphic Format)文件 .PNG image/png
BMP(Bitmap)文件 .BMP application/x-MS-bmp
GIF(Graphics Interchange Format)文件 .GIF image/gif
XML(EXtensible Markup Language)文件 .XML text/xml

常用類型文件的MIME

【編輯推薦】

  1. 在JSP中獲取數(shù)據(jù)庫(kù)連接
  2. 介紹JSP Action的使用
  3. 簡(jiǎn)化JSP表達(dá)式中代碼
  4. 詳解JSP向Servlet轉(zhuǎn)換
  5. 簡(jiǎn)單介紹JSP元素教程
責(zé)任編輯:佚名 來(lái)源: IT168
相關(guān)推薦

2009-07-06 17:46:25

JSP HTTP服務(wù)器

2009-07-03 13:05:47

JSP HTTP服務(wù)器

2009-07-06 17:56:12

JSP HTTP服務(wù)器

2009-07-06 17:34:38

JSP HTTP服務(wù)器

2009-07-06 17:40:05

JSP HTTP服務(wù)器

2009-07-06 14:05:11

JSP服務(wù)器

2009-07-06 18:02:34

JSP服務(wù)器

2020-06-17 21:39:11

HTTP協(xié)議服務(wù)器

2009-07-02 17:17:03

2010-05-21 11:50:54

IIS服務(wù)器

2019-08-22 15:26:24

HTTP服務(wù)器Python

2019-07-04 15:00:32

PythonHTTP服務(wù)器

2017-11-10 08:58:49

Web服務(wù)器應(yīng)用程序

2018-10-09 09:28:12

HTTPHTTP協(xié)作服務(wù)器

2009-09-17 13:23:22

NIS服務(wù)器

2021-08-04 07:26:07

IIS服務(wù)器反向代理

2015-10-08 09:38:24

HTTP網(wǎng)絡(luò)協(xié)議文件傳輸

2018-06-15 10:25:43

Python HTTPFTP服務(wù)器

2015-09-29 09:25:20

HTTP網(wǎng)絡(luò)協(xié)議

2018-12-06 09:23:33

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)