Identifying file type in Java

前端 未结 6 1130
盖世英雄少女心
盖世英雄少女心 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:35

    If you are already using Spring this works for csv and excel:

    
    import org.springframework.mail.javamail.ConfigurableMimeFileTypeMap;
    
    import javax.activation.FileTypeMap;
    import java.io.IOException;
    
    public class ContentTypeResolver {
    
        private FileTypeMap fileTypeMap;
    
        public ContentTypeResolver() {
            fileTypeMap = new ConfigurableMimeFileTypeMap();
        }
    
        public String getContentType(String fileName) throws IOException {
            if (fileName == null) {
                return null;
            }
            return fileTypeMap.getContentType(fileName.toLowerCase());
        }
    
    }
    
    

    or with javax.activation you can update the mime.types file.

提交回复
热议问题