我正在尝试使用 opencsv 来解析这样的 csv 文件:
2020-09-18 06:50:00.000000
我正在尝试按照本教程添加解析的数据:https ://attacomsian.com/blog/spring-boot-upload-parse-csv-file 。这是我的模型:
public class MyIndPrd implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@CsvBindByName
private String service;
@CsvBindByName
private OffsetDateTime time;
@CsvBindByName
private Long nbAppels;
@CsvBindByName
private Double tempsDeReponseMoyenMillisecondes;
@CsvBindByName
private Long volume;
@CsvBindByName
private Double tempsDeReponseMoyenSecondes;
}
我尝试解析 offsetDateTime
通过做 :
OffsetDateTime odt = OffsetDateTime.parse (myIndPrds.get (i) .getTime ());
在录制之前
但你似乎不想这样做
@PostMapping("/upload-csv-file")
public String uplaodCSVFile(@RequestParam("file") MultipartFile file, Model model){
if (file.isEmpty()){
model.addAttribute("message", "Veuillez selectionner un fichier csv à importer.");
model.addAttribute("status", false);
}else try (Reader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
// create csv bean reader
CsvToBean<MyIndPrd> csvToBean = new CsvToBeanBuilder(reader)
.withType(MyIndPrd.class)
.withSeparator(';')
.withIgnoreLeadingWhiteSpace(true)
.build();
// convert CsvToBean object
List<MyIndPrd> myIndPrds = csvToBean.parse();
for (int i = 0;i<myIndPrds.size();i++){
OffsetDateTime odt = OffsetDateTime.parse(myIndPrds.get(i).getTime());
MyIndPrd ind = new MyIndPrd();
ind.setService(myIndPrds.get(i).getService());
ind.setTime(odt);
ind.setNbAppels(Long.valueOf(myIndPrds.get(i).getNbAppels()));
ind.setVolume(Long.valueOf(myIndPrds.get(i).getVolume()));
ind.setTempsDeReponseMoyenMillisecondes(Double.valueOf(myIndPrds.get(i).getTempsDeReponseMoyenMillisecondes()));
ind.setTempsDeReponseMoyenSecondes(Double.valueOf(myIndPrds.get(i).getTempsDeReponseMoyenSecondes()));
iMyIndPrdService.saveMyData(ind);
}
model.addAttribute("myIndProdCsv", myIndPrds);
model.addAttribute("status", true);
} catch (Exception ex) {
model.addAttribute("message", "An error occurred while processing the CSV file.");
model.addAttribute("status", false);
}
return "mon-dasboard";
}
谢谢你的帮助