Content API
Content 模組提供檔案內容類型(MIME Type)檢測功能。
套件: io.leandev.appfuse.content
核心類別
ContentDetector
內容類型檢測器。
public class ContentDetector {
MediaType detect(Path path) throws IOException
MediaType detect(InputStream input) throws IOException
MediaType detect(byte[] bytes) throws IOException
}
使用範例
從檔案路徑檢測
ContentDetector detector = new ContentDetector();
Path imagePath = Paths.get("/uploads/photo.jpg");
MediaType mediaType = detector.detect(imagePath);
System.out.println(mediaType.toString()); // image/jpeg
從 InputStream 檢測
ContentDetector detector = new ContentDetector();
try (InputStream input = new FileInputStream(file)) {
MediaType mediaType = detector.detect(input);
if (mediaType.getType().equals("image")) {
// 處理圖片
}
}
從 byte[] 檢測
ContentDetector detector = new ContentDetector();
byte[] fileContent = Files.readAllBytes(path);
MediaType mediaType = detector.detect(fileContent);
String mimeType = mediaType.toString();
搭配檔案上傳
@PostMapping("/upload")
public ResponseEntity<?> upload(@RequestParam MultipartFile file) {
ContentDetector detector = new ContentDetector();
try {
MediaType mediaType = detector.detect(file.getInputStream());
// 驗證檔案類型
if (!isAllowedType(mediaType)) {
return ResponseEntity.badRequest().body("Invalid file type");
}
// 儲存檔案...
} catch (IOException e) {
return ResponseEntity.internalServerError().build();
}
}
private boolean isAllowedType(MediaType mediaType) {
return mediaType.getType().equals("image") ||
mediaType.toString().equals("application/pdf");
}
支援的 MIME 類型
圖片
| 副檔名 | MIME Type |
|---|---|
| jpg, jpeg | image/jpeg |
| png | image/png |
| gif | image/gif |
| webp | image/webp |
| bmp | image/bmp |
| svg | image/svg+xml |
| ico | image/x-icon |
| tiff | image/tiff |
| avif | image/avif |
| heic, heif | image/heic |
文檔
| 副檔名 | MIME Type |
|---|---|
| application/pdf | |
| txt | text/plain |
| csv | text/csv |
| xml | application/xml |
| json | application/json |
| md | text/markdown |
Office
| 副檔名 | MIME Type |
|---|---|
| doc | application/msword |
| docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| xls | application/vnd.ms-excel |
| xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| ppt | application/vnd.ms-powerpoint |
| pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
壓縮
| 副檔名 | MIME Type |
|---|---|
| zip | application/zip |
| gz | application/gzip |
| tar | application/x-tar |
| 7z | application/x-7z-compressed |
| rar | application/vnd.rar |
網頁
| 副檔名 | MIME Type |
|---|---|
| html, htm | text/html |
| css | text/css |
| js | text/javascript |
| ts | text/typescript |
| jsx, tsx | text/javascript |
多媒體
| 副檔名 | MIME Type |
|---|---|
| mp4 | video/mp4 |
| avi | video/x-msvideo |
| mov | video/quicktime |
| webm | video/webm |
| mp3 | audio/mpeg |
| wav | audio/wav |
| ogg | audio/ogg |
檢測策略
ContentDetector 使用混合策略檢測 MIME 類型:
- Java NIO -
Files.probeContentType() - 魔術位元組 -
URLConnection.guessContentTypeFromStream() - 副檔名映射 - 內建副檔名對照表
異常處理
ContentException
try {
MediaType mediaType = detector.detect(path);
} catch (ContentException e) {
// 檢測失敗
log.error("Failed to detect content type: {}", e.getMessage());
} catch (IOException e) {
// IO 錯誤
log.error("IO error: {}", e.getMessage());
}
最佳實踐
- 驗證上傳檔案 - 不要只信任副檔名,檢測實際內容
- 白名單驗證 - 只允許預期的 MIME 類型
- 處理檢測失敗 - 未知類型預設為
application/octet-stream