maven引入
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.9.0</version>
</dependency>
代码实现
//测试使用的csv下载地址
String csvUrl = " http://sdxkyy.topwellsoft.com:8080/test.csv"
//创建CSVFormat对象
CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',')
//初始化CSVParser 对象
CSVParser parse = CSVParser.parse(new URL(csvUrl), Charset.forName("GBK"), format)
/*遍历解析的csv每一行数据,转换成实体*/
for (CSVRecord record : parse) {
log.info(record.toString())
}
finally {
if (null != parse) {
parse.close();
}
}