0

I am trying to upload a csv file and i intend to write success to the uploaded csv if the upload goes successfully. below is my code(this is not working as of now)

@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";
    }
fiddle
  • 1,095
  • 5
  • 18
  • 33
  • Please post: the current behavior, the expected one, any error messages (stacktraces) you get –  Aug 13 '15 at 17:03
  • The current behavior is :the record gets added to the database but the status is not printed on the csv. – fiddle Aug 13 '15 at 17:09
  • 1
    Imagine I send you a letter (by post and keep a copy) you get it, open it and write "A" on line 12 of what's written. On my side, I don't see the A in my copy, that's the same here. So if you want to modify the uploaded file you have to somehow return it to the uploader –  Aug 13 '15 at 17:20

1 Answers1

1

I am using this code to import csv file and data is store into database.Add opencsv jar file to your build path.

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";
    }
NewUser
  • 3,729
  • 10
  • 57
  • 79
sanjay
  • 437
  • 3
  • 17
  • where is this code printing "success" to the CSV. I dont need the code upload the csv, i am done with that part. I just need help to write "success" to every row of the csv which was uploaded in the database. Thanks! – fiddle Aug 15 '15 at 18:28
  • I was wondering if you coud should how to use printwriter or something in such a screnario. – fiddle Aug 15 '15 at 18:47