2

I need to upload a file. Before I upload it, I need to know if the file is either csv or excel file(xlsx). If it is csv or excel file I will continue else exit. How can I know the type of a file in java by using its path.

R.Y.H
  • 73
  • 3
  • 10

2 Answers2

2

You can use probeContentType(Path path) for this.

Probes the content type of a file.

If the content-type is text/csv it is a .csv file. If the content-type is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet it is a .xlsx file.

You should try something like:

File file = new File("some path");
Path filePath = file.toPath();
String contentType = probeContentType(filePath);
if("text/csv".equals(contentType)) { 
  ...
}
thegauravmahawar
  • 2,802
  • 3
  • 14
  • 23
-1
  <?php
  $mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
    if(in_array($_FILES['file']['type'],$mimes)){
      // do something
    } else {
      die("Sorry, mime type not allowed");
    }

?>