简体   繁体   中英

Jquery progress bar while uploading record into database in java

I have thousands of record which are stored in a excel sheet and I need to upload those records into database, And currently I am using Spring controller class for upload, And inside my class I use simple BufferedOutputStream and FileReader classes, So my requirement is I need to show a Jquery progress-bar including percentages while uploading my data into database.

像下面的屏幕。

Link here.

My sample code.

 String rootPath = request.getSession().getServletContext().getRealPath("/");
    File dir = new File(rootPath + File.separator + "uploadedfile");
    if (!dir.exists()) {
        dir.mkdirs();
    }

    File serverFile = new File(dir.getAbsolutePath() + File.separator + form.getEmpFile().getOriginalFilename());

    try {
        try (InputStream is = form.getEmpFile().getInputStream();
                BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile))) {
            int i;
            //write file to server
            while ((i = is.read()) != -1) {
                stream.write(i);
            }
            stream.flush();
        }
    }
    catch (IOException e) {
        model.addAttribute("msg", "failed to process file because : " + e.getMessage());
    }
    String[] nextLine;
    try (FileReader fileReader = new FileReader(serverFile); CSVReader reader = new CSVReader(fileReader, ';', '\'', 1);) {
        while ((nextLine = reader.readNext()) != null) {
            for (int i = 0; i < nextLine.length; i++) {
                nextLine[i] = nextLine[i].trim();
                if (!nextLine[i].equals("")) {
                    String[] data = nextLine[i].split(",");

Maybe this can help:

1-

 public class ExtractController { 
   ******** 
  // I created a global variable here
   int percentage = 0; 
   Workbook workbook;

 @RequestMapping(value ="/uploadExcel", method = RequestMethod.POST)
 public @ResponseBody String uploadExcel(Model model, @RequestParam("excelfile") MultipartFile excelfile,
        HttpServletResponse response) {
 **********
     try {
        int i = 0;
 ********* 
          while (i <= worksheet.getLastRowNum()) {
            percentage = Math.round(((i * 100) /  worksheet.getLastRowNum()));
   Row row = worksheet.getRow(i++);
  }
    *********
     }catch (Exception e) {
        e.printStackTrace();
    }
 return "extract";
}

 @RequestMapping(value = "/getpercent", method = RequestMethod.GET)
  public @ResponseBody String getPerc(@RequestParam("param") String param) {
************

 // you  return the value of the global variable when the action is called
    return percrentage;

   }
 }

1-

 $.ajax({
                 url : 'uploadExcel',
                type : 'POST',
                data : new FormData(this),
                beforeSend : function() {
                                                  $('#valid').attr("disabled", true);
                                                    // I call my loop function 
                                                    loop();
                                                },
                success : function(data) {
                                                       $('#valid').attr("disabled", false);
                                                }
                                            });


function loop() {               
                    $.ajax({

                        url : 'getpercent',

                        type : 'GET',

                        success : function(response) { 
                             count = response;
                                // this function updates my progress bar with  the new value 
                                change(count);
                                *********
                        }

                    });
                    var time = 2000;

                if(count < 100){
                    setTimeout(loop, time);
                }


                }
                ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM