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.
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)) {
...
}
$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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.