MySQL 之 LOAD DATA INFILE 快速导入数据

https://www.cnblogs.com/waynechou/p/7794939.html#_label0

https://blog.csdn.net/brucelin_good/article/details/43704905

public static String loadSql(String filePath, String table, String split, String rEnd) {
     StringBuilder sql = new StringBuilder();
     sql.append("LOAD DATA INFILE")
        .append(" ").append("'").append(filePath).append("'").append(" ")
        //IGNORE 忽略错误行
        .append("IGNORE INTO TABLE").append(" ").append(table).append(" ")
        .append("FIELDS TERMINATED BY").append(" ").append("'").append(split).append("'").append(";");
     return sql.toString();
}

后台

 public AjaxResult importData(@RequestParam(value = "info", required = false) MultipartFile file) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            String fileName = file.getOriginalFilename().toLowerCase();
            String fileType = fileName.substring(fileName.lastIndexOf("."));
            if (!".txt".equals(fileType)) {
                return error("文件类型错误");
            }
            String uploadName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            String path = ImeiConfig.getCommonFilePath() + "/" + uploadName;
            br = new BufferedReader(new InputStreamReader(file.getInputStream(), "utf-8"));
            bw = new BufferedWriter(new FileWriter(path));
            String lineTxt = null;
            SimpleCrypt instance = SimpleCrypt.INSTANCE;
            while ((lineTxt = br.readLine()) != null) {
                String[] split = lineTxt.split("\$");
                if (split.length == 99) {
                    String f2 = instance.encrypt(split[2], getKey);
                    String f3 = instance.encrypt(split[3], getKey);
                    split[2] = f2;
                    split[3] = f3;
                    String join = StringUtils.join(split, "$");
                    bw.write(join);
                    bw.newLine();
                    bw.flush();
                }
            }
            Runtime.getRuntime().exec("chmod 755 " + path);
            String sql = SqlUtil.loadSql(path, "base_terminal_model_encrypt", "$", "");
            int size = baseTerminalModelEncryptService.insertBaseTerminalModelBatch(sql.substring(0, sql.length() - 1));
            FileUtil.deleteFile(path);
            return AjaxResult.success("导入成功" + size + "条!");
        } catch (Exception e) {
            log.error("导入报名信息失败:{}", e.getMessage());
            return error("系统繁忙,导入失败!");
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                log.error("导入文件读入失败:{}", e.getMessage());
            }
            try {
                bw.close();
            } catch (IOException e) {
                log.error("导入文件写入失败:{}", e.getMessage());
            }
        }
    }

  

原文地址:https://www.cnblogs.com/person008/p/15182660.html