MIME Type is null for log files

I am developing a Spring REST service using Spring Boot, that is, in a nutshell, sending emails. It supports attachments.

I got a validator class that ensures that only expected attachment types are accepted, through MIME type (content type).

Supports MIME type are:

  • application/pdf application/msword
  • application/vnd.openxmlformats-officedocument.wordprocessingml.document
  • application/vnd.ms-excel
  • application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
  • text/plain
  • It works wonders for all file types like doc, docx, xls, xlsx, txt and pdf. Validator is also able to reject unsupported file types like exe, jpeg, etc.

    The problem is that when I take a txt file (let's call it test.txt), and then rename it to test.log, the MIME type is now null. The same bevavior is observed when creating a brand new file directly with .log file extension.

    Validation code is the following:

    for (MultipartFile file : attachments) {            
                Matcher matcher = pattern.matcher(file.getContentType());
                if (!matcher.matches()) {
                    errors.reject("Disallowed attachment file type.");
                }
            }
    

    REST endpoint method signature:

    @PostMapping
        public ResponseEntity<String> sendMail(
                @RequestPart(value = "mail", required = true) @Valid Mail mail,
                BindingResult result,
                @RequestPart(value = "attachments", required = false) MultipartFile[] attachments
        ) throws CheckedMailException
    

    Any recommendation about how to manage this would be appreciated.

    Thank you

    链接地址: http://www.djcxy.com/p/45488.html

    上一篇: Mime不能用于回形针

    下一篇: 日志文件的MIME类型为空