java - Differentiate between .csv and false .csv -


i have method uploads csv file. works fine when work normal csv file.

the problems comes when reads file (say, pdf) renamed .csv file extension , doesn't throw exception.

can me in differentiating bona fide csv file , "fake" csv file .csv file extension?


update following comment:

here code:

csvreader csvreader = new csvreader(); arraylist <arraylist<string>> arrdata=null; string path="c:/users/avinash/desktop/asset.csv"; arrdata=csvreader.readcsv(path,printinconsole); 

the above code works fine if asset.csv real csv file. other format file (say asset.pdf) renamed asset.csv, doesn't throw exception reads junk values.

can in differentiating these 2 different file types?

i tried jmimemagic fails give correct mime type of spreadsheets open office.

how differentiate bona fide csv file , "fake" csv file .csv file extension?

you can not in straight forward way, since each file can interpreted csv file (records separated newline, fields separated comma, binary files, using character set, can read "valid" csv files). if, example, file not contain newline or comma characters, end single record/single field result containing whole file.

in order check if valid cvs file (for use case), need validate result has been read csv file using constraints know particular data needs fullfil. can, example,

  • validate header / title values
  • validate number of fields expect in each record
  • validate maximum/minimum field lengths , record lengths
  • validate character set - data contain invalid characters?
  • validate whether field data in specific range?

btw: should these validations anyway, in order avoid malicious data being imported application.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -