Http協議中的媒體型別
阿新 • • 發佈:2020-08-06
介紹
Web伺服器會為所有HTTP物件資料附加一個MIME型別。當Web瀏覽器從伺服器中取回一個物件時,會去檢視相關的MIME型別,看看它是否知道應該如何處理這個物件。大多數瀏覽器都可以處理數百種常見的物件型別:顯示圖片檔案、解析並格式化HTML檔案、通過計算機音效卡播放音訊檔案,或者執行外部外掛軟體來處理特殊格式的資料。
圖1-1 與資料內容一同回送的MIME型別
案例
下面是通過curl命令向伺服器請求excel文件的過程,伺服器返回的物件型別是 application/x-msdownload
[cdh140 ~]$ curl -v http://12.32.7.100:8008/monitor/download * About to connect() to 12.32.7.100 port 8008 (#0) * Trying 12.32.7.100... * Connected to 12.32.7.100 (10.22.5.140) port 8008 (#0) > GET /monitor/download HTTP/1.1 > User-Agent: curl/7.29.0 > Host: 12.32.7.100:8008 > Accept: */* > < HTTP/1.1 200 < Content-Disposition: attachment;filename=????.xlsx < Content-Type: application/x-msdownload < Content-Length: 5120 < Date: Thu, 06 Aug 2020 01:30:54 GMT
常見MIME型別
主型別
型別 | 描述 |
---|---|
application | 應用程式特有的格式 |
audio | 音訊格式 |
chemical | 化學資料集 |
image | 圖片格式 |
message | 報文格式 |
model | 三維模型格式 |
multipart | 多部分物件集合 |
text | 文字格式 |
video | 視訊電影格式 |
子型別
MIME型別是一種文字標記,表示一種主要的物件型別和一個特定的子型別,中間由一條斜槓來分隔。
拓展名 | 文件型別 | MIME型別 |
---|---|---|
.htm .html | HyperText Markup Language (HTML) | text/html |
.txt | Text, (generally ASCII or ISO 8859-n) | text/plain |
.json | JSON format | application/json |
.png | Portable Network Graphics | image/png |
.jpeg .jpg | JPEG images | image/jpeg |
.xls | Microsoft Excel | application/vnd.ms-excel |
.xlsx | Microsoft Excel (OpenXML) | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
Adobe Portable Document Format (PDF) | application/pdf | |
.zip | ZIP archive | application/zip |
更多的MIME型別參考火狐瀏覽器官方文件:
用Java傳送HTTP請求傳輸Excel檔案
/**
* 下載Excel報告
*
* @param request
* @param response
* @throws IOException
*/
public void downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
InputStream is = new FileInputStream(FileConfig.FILE_PATH + ".xlsx");
HSSFWorkbook workbook = new HSSFWorkbook(is);
OutputStream out = response.getOutputStream();
String fileName = FileConfig.FILE_PATH;// 檔名
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "UTF-8"));
workbook.write(out);
out.close();
response.getOutputStream().flush();
response.getOutputStream().close();
}