gpt4 book ai didi

java - 使用 Spring MVC 上传文件不起作用

转载 作者:行者123 更新时间:2023-12-01 11:14:27 26 4
gpt4 key购买 nike

我正在尝试上传 csv 文件,如果上传成功,我打算将<​​strong>成功写入到上传的 csv 中。下面是我的代码(目前还不起作用)

@RequestMapping(value = "/submitUploadCarForm")
public String uploadCar(CarUploadForm uploadform,HttpServletRequest request, Model model, BindingResult result) {

try {

CommonsMultipartFile file = uploadform.getFileData();
// parse csv file to list
csvList = CarCSVUtil.getCSVInputList(file.getInputStream());

for (CarCSVFileInputBean inputRecord : csvList) {

Car carentity = new Car();

carentity.setId(inputRecord.getId());
carentity.setName(inputRecord.getcarName());
carentity.setShortName(inputRecord.getcarShortName());
carentity.setEnvironment(inputRecord.getEnvironment());
carentity = this.Service.saveCar(carentity);

CarConfig compcfg = new CarConfig();
compcfg.setCar(carentity);
compcfg.setCarType(pickCarType(carTypes,inputRecord.getcarType()));

this.Service.saveCompCfg(compcfg);
inputRecord.setuploadstatus("success");<--- This is where i need help

}

}

catch (Exception e) {
e.printStackTrace();
result.rejectValue("name", "failureMsg","Error while uploading ");
model.addAttribute("failureMsg", "Error while uploading ");
}
return "view";
}

最佳答案

我使用此代码导入 csv 文件并将数据存储到数据库中。将 opencsv jar 文件添加到您的构建路径。

public String importCSV(@RequestParam("file") MultipartFile file,
HttpServletRequest req) throws IOException {
CsvToBean csv = new CsvToBean();
CSVReader reader = new CSVReader(new InputStreamReader(
file.getInputStream()), ',', '\"', 1);
ColumnPositionMappingStrategy maping = new ColumnPositionMappingStrategy();
maping.setType(tbBank.class);
String[] column = { "bankid", "bankname", "bankbranch" };
maping.setColumnMapping(column);
List banklist = csv.parse(maping, reader);
for (Object obj : banklist) {
tbBank bank = (tbBank) obj;
projectservice.insertBank(bank);
}
return "redirect:/bankview";
}

关于java - 使用 Spring MVC 上传文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31994348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com