CSV文件上传解析
Posted G_whang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CSV文件上传解析相关的知识,希望对你有一定的参考价值。
上传CSV文件代码如下
引入POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- javacsv -->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
<!-- commons-lang StringUtils -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<!-- slf4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
pojo
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 学生类
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
/** 学号 */
private String studentId;
/** 姓名 */
private String name;
/** 总分 */
private Integer totalScore;
}
工具类 现在不是很智能,如果上传内容不一致的话,需要修改工具类读取
import com.csvreader.CsvReader;
import com.example.csvuploadtest.pojo.Student;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* csv 文件读取工具类
*/
public class CsvUtils {
/** 日志 */
private static final Logger log = LoggerFactory.getLogger(CsvUtils.class);
/**
* 文件上传
* @param file
* @return
* @throws FileNotFoundException
*/
public static List<Student> achievementRead(MultipartFile file){
List<Student> result = new ArrayList<Student>();
try {
byte [] byteArr=file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteArr);
/** 创建CSV读对象 */
CsvReader csvReader = new CsvReader(inputStream, Charset.forName("utf-8"));
/** 读表头 */
csvReader.readHeaders();
while (csvReader.readRecord()){
Student student = new Student();
/** 读一整行 */
String rawRecord = csvReader.getRawRecord();
/** 分割 */
String[] split = rawRecord.split(",");
/** 学号 */
student.setStudentId(split[0]);
/** 姓名 */
student.setName(split[1]);
/** 各科成绩 */
int chinese = Integer.parseInt(split[2]);
int math = Integer.parseInt(split[3]);
int english = Integer.parseInt(split[4]);
int physics = Integer.parseInt(split[5]);
int chemistry = Integer.parseInt(split[6]);
int biology = Integer.parseInt(split[7]);
student.setTotalScore(biology + chemistry + chinese + english + math + physics);
/** 放入list */
result.add(student);
}
}catch (Exception e){
log.error("读取CSV文件异常 异常信息为:"+e);
throw new RuntimeException("读取CSV文件异常 异常信息为:"+e);
}
return result;
}
/**
* 读csv文件
* @param filePath
*/
public static List<Student> achievementRead(String filePath){
List<Student> result = new ArrayList<Student>();
if (StringUtils.isNotBlank(filePath)) {
try {
/** 创建CSV读对象 */
CsvReader csvReader = new CsvReader(filePath,',', Charset.forName("utf-8"));
/** 读表头 */
csvReader.readHeaders();
while (csvReader.readRecord()){
Student student = new Student();
/** 读一整行 */
String rawRecord = csvReader.getRawRecord();
/** 分割 */
String[] split = rawRecord.split(",");
/** 学号 */
student.setStudentId(split[0]);
/** 姓名 */
student.setName(split[1]);
/** 各科成绩 */
int chinese = Integer.parseInt(split[2]);
int math = Integer.parseInt(split[3]);
int english = Integer.parseInt(split[4]);
int physics = Integer.parseInt(split[5]);
int chemistry = Integer.parseInt(split[6]);
int biology = Integer.parseInt(split[7]);
student.setTotalScore(biology + chemistry + chinese + english + math + physics);
/** 放入list */
result.add(student);
}
} catch (IOException e) {
log.error("读取文件异常:"+ e.getMessage());
}
} else {
log.error("文件路径不能为空!");
}
return result;
}
}
上传Controller
import com.alibaba.fastjson.JSONObject;
import com.example.csvuploadtest.pojo.Student;
import com.example.csvuploadtest.util.CsvUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 文件上传验证
*/
@RestController
@RequestMapping("/csv")
public class CsvControllerTest {
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file){
// 调用文件上传解析工具类
List<Student> students = CsvUtils.achievementRead(file);
return JSONObject.toJSONString(students);
}
}
验证
完整代码
以上是关于CSV文件上传解析的主要内容,如果未能解决你的问题,请参考以下文章
正确上传不正确的 CSV 文件的 HTTP REST API 状态代码
python 解析用于KnowBe4电子邮件上传的csv文件