Restful Spring Boot アプリケーションに pdf ファイルのアップロードを実装しようとしています。
次の方法があります。
@RequestMapping(value = FILE_URL, method = RequestMethod.POST)
public ResponseDTO submitPDF(
@ModelAttribute("file") FileDTO file) {
MediaType mediaType = MediaType.parseMediaType(file.getFile().getContentType());
System.out.println(file.getFile().getContentType());
System.out.println(mediaType);
System.out.println(mediaType.getType());
if(!"application/pdf".equals(mediaType.getType())) {
throw new IllegalArgumentException("Incorrect file type, PDF required.");
}
... more code here ...
}
FileDTO は、MultipartFile の単なるラッパーです。
次に、Postman を使用してform-data
本文付きのリクエストを POST します'file'=<filename.pdf>
上記の printlns のコンテンツ タイプは常にオクテット ストリームです。送信するファイルの種類 (png、pdf など) に関係なく、常にオクテット ストリームです。Postman で Content-Type ヘッダーとして具体的に設定application/pdf
すると、FileDTO 内の MultipartFile が null になります。
質問は、Spring Controller メソッドに何か問題があるのでしょうか、それとも Postman によってリクエストが正しく構築されていないだけですか?
Postman が Content-Type を正しく取得できない場合、実際のクライアント アプリがコンテンツ タイプを pdf に適切に設定することを期待できますか?