Identifying file type in Java

前端 未结 6 1174
盖世英雄少女心
盖世英雄少女心 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-09 00:01

    I hope this will help. Taken from an example not from mine:

    import javax.activation.MimetypesFileTypeMap;
    import java.io.File;
    
    class GetMimeType {
      public static void main(String args[]) {
        File f = new File("test.gif");
        System.out.println("Mime Type of " + f.getName() + " is " +
                             new MimetypesFileTypeMap().getContentType(f));
        // expected output :
        // "Mime Type of test.gif is image/gif"
      }
    

    }

    Same may be true for excel and csv types. Not tested.

提交回复
热议问题