跳至主要内容

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, jpegimage/jpeg
pngimage/png
gifimage/gif
webpimage/webp
bmpimage/bmp
svgimage/svg+xml
icoimage/x-icon
tiffimage/tiff
avifimage/avif
heic, heifimage/heic

文檔

副檔名MIME Type
pdfapplication/pdf
txttext/plain
csvtext/csv
xmlapplication/xml
jsonapplication/json
mdtext/markdown

Office

副檔名MIME Type
docapplication/msword
docxapplication/vnd.openxmlformats-officedocument.wordprocessingml.document
xlsapplication/vnd.ms-excel
xlsxapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet
pptapplication/vnd.ms-powerpoint
pptxapplication/vnd.openxmlformats-officedocument.presentationml.presentation

壓縮

副檔名MIME Type
zipapplication/zip
gzapplication/gzip
tarapplication/x-tar
7zapplication/x-7z-compressed
rarapplication/vnd.rar

網頁

副檔名MIME Type
html, htmtext/html
csstext/css
jstext/javascript
tstext/typescript
jsx, tsxtext/javascript

多媒體

副檔名MIME Type
mp4video/mp4
avivideo/x-msvideo
movvideo/quicktime
webmvideo/webm
mp3audio/mpeg
wavaudio/wav
oggaudio/ogg

檢測策略

ContentDetector 使用混合策略檢測 MIME 類型:

  1. Java NIO - Files.probeContentType()
  2. 魔術位元組 - URLConnection.guessContentTypeFromStream()
  3. 副檔名映射 - 內建副檔名對照表

異常處理

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());
}

最佳實踐

  1. 驗證上傳檔案 - 不要只信任副檔名,檢測實際內容
  2. 白名單驗證 - 只允許預期的 MIME 類型
  3. 處理檢測失敗 - 未知類型預設為 application/octet-stream

相關連結