java判断csv文件为空,使用Java的CSV文件验证

在读取CSV文件时遇到问题,如果上传的文件不是CSV,读取图片等会输出乱码。文章提到仅通过扩展名判断不安全,建议使用MIME类型检测,如MimeUtility或MagicMatch,读取文件内容判断是否为纯文本或CSV。
摘要由CSDN通过智能技术生成

I'm reading a file line by line, like this:

FileReader myFile = new FileReader(File file);

BufferedReader InputFile = new BufferedReader(myFile);

// Read the first line

String currentRecord = InputFile.readLine();

while(currentRecord != null) {

currentRecord = InputFile.readLine();

}

But if other types of files are uploaded, it will still read their contents. For instance, if the uploaded file is an image, it will output junk characters when reading the file. So my question is: how can I check the file is CSV for sure before reading it?

Checking extension of the file is kind of lame since someone can upload a file that is not CSV but has a .csv extension. Thanks in advance.

解决方案

Determining the MIME type of a file is not something easy to do, especially if ASCII sections can be

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值