identify the file extension using java

后端 未结 3 1114
遇见更好的自我
遇见更好的自我 2021-01-19 18:46

i have different format files in DB. i want to copy to my local machine.

how can i identify the file format (doc, xls, etc...)

Regards, krishna

Than

相关标签:
3条回答
  • 2021-01-19 18:55

    If your files are named according to convention, you can just parse the filename:

    String filename = "yourFileName";
    int dotPosition = filename.lastIndexOf(".");
    String extension = "";
    if (dotPosition != -1) {
        extension = filename.substring(dotPosition);
    }
    System.out.println("The file is of type: " + extension);
    

    That's the simplest approach, assuming your files are named using some kind of standard naming convention. The extensions could be proprietary to your system, even, as long as they follow a convention this will work.

    If you need to actually scan the file to get the format information, you will need to do some more investigation into document formats.

    0 讨论(0)
  • 2021-01-19 18:56

    You can use the Tika apache library. As Dmitri pointed out however, you may have incorrect results sometimes if detecting mime type from file headers or file extension.

    0 讨论(0)
  • 2021-01-19 19:08

    How are the files stored? Do you have filenames with extensions, or just the binary data?

    Mime Util has tools to detect format both from extensions and from magic headers, but of course that's never 100%.

    0 讨论(0)
提交回复
热议问题