Mit*_*hun 3 java csv web-applications
I have a scenario wherein the user uploads a file to the system. The only file that the system understands in a CSV, but the user can upload any type of file eg: jpeg, doc, html. I need to throw an exception if the user uploads anything other than CSV file.
Can anybody let me know how can I find if the uploaded file is a CSV file or not?
我能想到几种方法。
一种方法是尝试使用 UTF-8 解码文件。(它内置于 Java 中,并且可能也内置于 .NET 中。)如果文件正确解码,那么您至少知道它是某种文本文件。
一旦您知道它是一个文本文件,请解析每行中的各个字段并检查是否获得了预期的字段数。如果每行的字段数不一致,则您可能只有一个包含文本的文件,但未将其组织为行和字段。
否则你就有一个 CSV。然后您可以验证这些字段。