关于jquerry提交表单时遇到的问题

问题主要是。
我提交表单是没有问题的

但是我提交速度会很慢,或者可能是卡住了,然后当我刷新页面时,表单会再次提交..非常影响功能

关于为什么提交速度很慢因为我是提交的excel文件转数据库表。

img

img

感谢邀请,题主的问题初步判断是未关闭流导致的问题。

1、当第一次进页面时,前端是没有和后端建立会话链接的
2、提交完第一次后,若是文件加载到内存,但是这时还没关闭流,会话还存在,前端刷新页面还会有流文件传输的动作。
3、题主可以把后端解析excel的代码贴出来。

若有帮助,记得采纳哦。

excel的大小多少呢

刷新页面时,表单再次提交?
那刚进入页面时,表单会自动提交吗?

原因应该就是接口慢,如果接口不能解决慢的问题,前端可能也没什么办法,可以加个上传中的状态来提示用户, 应该会友好点


package com.yyhbgz;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.yyhbgz.pojo.Employee;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
public class ReadExcel {
    //总行数
    private int totalRows = 0;
    //总条数
    private int totalCells = 0;
    //错误信息接收器
    private String errorMsg;
    //构造方法
    public ReadExcel(){}
    //获取总行数
    public int getTotalRows()  { return totalRows;}
    //获取总列数
    public int getTotalCells() {  return totalCells;}
    //获取错误信息
    public String getErrorInfo() { return errorMsg; }
    /**
     * 读EXCEL文件,获取信息集合
     * @param
     * @return
     */
    public List<Employee> getExcelInfo(MultipartFile mFile) {
        String fileName = mFile.getOriginalFilename();//获取文件名
        System.out.println("文件名"+fileName);
        List<Employee> employeeList = null;
        try {
            if (!validateExcel(fileName)) {// 验证文件名是否合格
                return null;
            }
            boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
            if (isExcel2007(fileName)) {
                isExcel2003 = false;
            }
            employeeList = createExcel(mFile.getInputStream(), isExcel2003);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return employeeList;
    }
    /**
     * 根据excel里面的内容读取客户信息
     * @param is 输入流
     * @param isExcel2003 excel是2003还是2007版本
     * @return
     * @throws IOException
     */
    public List<Employee> createExcel(InputStream is, boolean isExcel2003) {
        List<Employee> employeeList = null;

        try{
            Workbook wb = null;
            if (isExcel2003) {// 当excel是2003时,创建excel2003
                wb = new HSSFWorkbook(is);
            } else {// 当excel是2007时,创建excel2007
                wb = new XSSFWorkbook(is);
            }
            employeeList = readExcelValue(wb);// 读取Excel里面客户的信息
        } catch (IOException e) {
            e.printStackTrace();
        }
        return employeeList;
    }
    /**
     * 读取Excel里面客户的信息
     * @param wb
     * @return
     */
    private List<Employee> readExcelValue(Workbook wb) {
        // 得到第一个shell
        Sheet sheet = wb.getSheetAt(0);
        System.out.println("gaolei dayin============" +sheet);
        // 得到Excel的行数
        this.totalRows = sheet.getPhysicalNumberOfRows();
        System.out.println("行数======="+this.totalRows);
        // 得到Excel的列数(前提是有行数)
        if (totalRows > 1 && sheet.getRow(0) != null) {
            this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
            System.out.println("总列数=========="+this.totalCells);
        }
        List<Employee> empList = new ArrayList<Employee>();
        // 循环Excel行数
        for (int r = 1; r < totalRows; r++) {
            Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            Employee employee = new Employee();
            // 循环Excel的列
            for (int c = 0; c < this.totalCells; c++) {
                Cell cell = row.getCell(c);
                if (null != cell) {


                    if (c == 0){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String eid = String.valueOf(cell.getNumericCellValue());
                            employee.setEid(eid);
                        }else{
                            employee.setEid(cell.getStringCellValue());
                        }
                    }
                    else if (c == 1){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String age = String.valueOf(cell.getNumericCellValue());
                            employee.setAge(age);
                        }else{
                            employee.setAge(cell.getStringCellValue());
                        }
                    }
                    else if (c == 2){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String ename = String.valueOf(cell.getNumericCellValue());
                            employee.setEname(ename);
                        }else{
                            employee.setEname(cell.getStringCellValue());
                        }
                    }
                    else if (c == 3){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String gsalary = String.valueOf(cell.getNumericCellValue());
                            employee.setGsalary(gsalary);
                        }else{
                            employee.setGsalary(cell.getStringCellValue());
                        }
                    }
                    else if (c == 4){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String bsalary = String.valueOf(cell.getNumericCellValue());
                            employee.setBsalary(bsalary);
                        }else{
                            employee.setBsalary(cell.getStringCellValue());
                        }
                    }
                    else if (c == 5){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String allowance = String.valueOf(cell.getNumericCellValue());
                            employee.setAllowance(allowance);
                        }else{
                            employee.setAllowance(cell.getStringCellValue());
                        }
                    }
                    else if (c == 6){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String asalary = String.valueOf(cell.getNumericCellValue());
                            employee.setAsalary(asalary);
                        }else{
                            employee.setAsalary(cell.getStringCellValue());
                        }
                    }
                    else if (c == 7){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String osalary = String.valueOf(cell.getNumericCellValue());
                            employee.setOsalary(osalary);
                        }else{
                            employee.setOsalary(cell.getStringCellValue());
                        }
                    }
                    else if (c == 8){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String bonus = String.valueOf(cell.getNumericCellValue());
                            employee.setBonus(bonus);
                        }else{
                            employee.setBonus(cell.getStringCellValue());
                        }
                    }
                    else if (c == 9){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String attendance = String.valueOf(cell.getNumericCellValue());
                            employee.setAttendance(attendance);
                        }else{
                            employee.setAttendance(cell.getStringCellValue());
                        }
                    }
                    else if (c == 10){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String overtime = String.valueOf(cell.getNumericCellValue());
                            employee.setOvertime(overtime);
                        }else{
                            employee.setOvertime(cell.getStringCellValue());
                        }
                    }
                    else if (c == 11){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String deduction = String.valueOf(cell.getNumericCellValue());
                            employee.setDeduction(deduction);
                        }else{
                            employee.setDeduction(cell.getStringCellValue());
                        }
                    }
                    else if (c == 12){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String sick = String.valueOf(cell.getNumericCellValue());
                            employee.setSick(sick);
                        }else{
                            employee.setSick(cell.getStringCellValue());
                        }
                    }
                    else if (c == 13){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String things = String.valueOf(cell.getNumericCellValue());
                            employee.setThings(things);
                        }else{
                            employee.setThings(cell.getStringCellValue());
                        }
                    }
                    else if (c == 14){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String absenteeism = String.valueOf(cell.getNumericCellValue());
                            employee.setAbsenteeism(absenteeism);
                        }else{
                            employee.setAbsenteeism(cell.getStringCellValue());
                        }
                    }
                    else if (c == 15){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String other = String.valueOf(cell.getNumericCellValue());
                            employee.setOther(other);
                        }else{
                            employee.setOther(cell.getStringCellValue());
                        }
                    }
                    else if (c == 16){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String ssalary = String.valueOf(cell.getNumericCellValue());
                            employee.setSsalary(ssalary);
                        }else{
                            employee.setSsalary(cell.getStringCellValue());
                        }
                    }
                    else if (c == 17){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String high = String.valueOf(cell.getNumericCellValue());
                            employee.setHigh(high);
                        }else{
                            employee.setHigh(cell.getStringCellValue());
                        }
                    }
                    else if (c == 18){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String eid1 = String.valueOf(cell.getNumericCellValue());
                            employee.setEid1(eid1);
                        }else{
                            employee.setEid1(cell.getStringCellValue());
                        }
                    }
                    else if (c == 19){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String ename1 = String.valueOf(cell.getNumericCellValue());
                            employee.setEname1(ename1);
                        }else{
                            employee.setEname1(cell.getStringCellValue());
                        }
                    }
                    else if (c == 20){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String job = String.valueOf(cell.getNumericCellValue());
                            employee.setJob(job);
                        }else{
                            employee.setJob(cell.getStringCellValue());
                        }
                    }
                    else if (c == 21){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String tax = String.valueOf(cell.getNumericCellValue());
                            employee.setTax(tax);
                        }else{
                            employee.setTax(cell.getStringCellValue());
                        }
                    }
                    else if (c == 22){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String pinsurance = String.valueOf(cell.getNumericCellValue());
                            employee.setPinsurance(pinsurance);
                        }else{
                            employee.setPinsurance(cell.getStringCellValue());
                        }
                    }
                    else if (c == 23){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String uinsurance = String.valueOf(cell.getNumericCellValue());
                            employee.setUinsurance(uinsurance);
                        }else{
                            employee.setUinsurance(cell.getStringCellValue());
                        }
                    }
                    else if (c == 24){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String hinsurance = String.valueOf(cell.getNumericCellValue());
                            employee.setHinsurance(hinsurance);
                        }else{
                            employee.setHinsurance(cell.getStringCellValue());
                        }
                    }
                    else if (c == 25){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String hfund = String.valueOf(cell.getNumericCellValue());
                            employee.setHfund(hfund);
                        }else{
                            employee.setHfund(cell.getStringCellValue());
                        }
                    }
                    else if (c == 26){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String eannuity = String.valueOf(cell.getNumericCellValue());
                            employee.setEannuity(eannuity);
                        }else{
                            employee.setEannuity(cell.getStringCellValue());
                        }
                    }
                    else if (c == 27){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String chouse = String.valueOf(cell.getNumericCellValue());
                            employee.setChouse(chouse);
                        }else{
                            employee.setChouse(cell.getStringCellValue());
                        }
                    }
                    else if (c == 28){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String others = String.valueOf(cell.getNumericCellValue());
                            employee.setOthers(others);
                        }else{
                            employee.setOthers(cell.getStringCellValue());
                        }
                    }
                    else if (c == 29){
                        if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
                            String actual = String.valueOf(cell.getNumericCellValue());
                            employee.setActual(actual);
                        }else{
                            employee.setActual(cell.getStringCellValue());
                        }
                    }

                }
            }
            // 添加到list
            empList.add(employee);
        }
        return empList;
    }

    /**
     * 验证EXCEL文件
     *
     * @param filePath
     * @return
     */
    public boolean validateExcel(String filePath) {
        if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
            errorMsg = "文件名不是excel格式";
            return false;
        }
        return true;
    }

    // @描述:是否是2003的excel,返回true是2003
    public static boolean isExcel2003(String filePath)  {
        return filePath.matches("^.+\\.(?i)(xls)$");
    }
    //@描述:是否是2007的excel,返回true是2007
    public static boolean isExcel2007(String filePath)  {
        return filePath.matches("^.+\\.(?i)(xlsx)$");
    }
}