Identifying file type in Java

前端 未结 6 1129
盖世英雄少女心
盖世英雄少女心 2020-12-08 23:24

Please help me to find out the type of the file which is being uploaded. I wanted to distinguish between excel type and csv.

MIMEType returns same for both of thes

6条回答
  •  独厮守ぢ
    2020-12-08 23:58

    I figured out a cheaper way of doing this with java.nio.file.Files

    public String getContentType(File file) throws IOException {
            return Files.probeContentType(file.toPath());
    }
    

    - or -

    public String getContentType(Path filePath) throws IOException {
            return Files.probeContentType(filePath);
    }
    

    Hope that helps.

    Cheers.

提交回复
热议问题