【问题标题】:Incorrect string value - JOOQ - Java不正确的字符串值 - JOOQ - Java
【发布时间】:2018-02-15 05:20:05
【问题描述】:

我正在尝试使用 jooq 将 csv 上传到我的 mysql 数据库中,但我遇到了以下错误。我尝试了网上建议的各种解决方案,但无法修复

Error: org.jooq.exception.DataAccessException: SQL [insert into `Test`.`testtable` <mydata> Incorrect string value: '\xEF\xBF\xBD15 ...' for column 'colum_Name' at row 1

我如何将 csv 上传到 jooq

create.loadInto(Tables.TableName)
                .onErrorIgnore()
                .loadCSV(new File("/tmp/uploaded-files/" + fileName), "UTF-8").<fields>.execute();

我确保文件在 utf-8 中,但是当有 UTF-8 字符记录时,无法保存在数据库中并抛出上述错误。我确保使用

file -i <filename>

前端ajax:

$.ajax({
    type: "POST",
    enctype: 'multipart/form-data',
    url: fileUploadUrl,
    data: dataforFileUpload,
    processData: false,
    contentType: false,
    cache: false,
    timeout: 600000,
    success: function (message) {
        console.log(message);
        alert(message);
        console.log("upload done "+ new Date());

    },
    error: function (e) {
        console.log("ERROR : ", e.responseText);
        alert("ERROR : "+  e.responseText);

    }
});

我从前端获取文件,通过 java rest 读取它

InputStream inputStream = listingFilePart.getBody(InputStream.class, null);

并在传递给jooq之前在本地系统中递归写入文件

PrintWriter fop = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8));

我将我的数据库设置为接受 utf-8 并且我验证了相同

【问题讨论】:

    标签: java mysql rest jooq


    【解决方案1】:

    我找到了问题。问题在 ajax 调用上。 我需要用enctype 发送字符集,比如

    $.ajax({
        type: "POST",
        enctype: 'multipart/form-data;charset=UTF-8',
        url: fileUploadUrl,
        data: dataforFileUpload,
        processData: false,
        contentType: false,
        cache: false,
        timeout: 600000,
        success: function (message) {
            console.log(message);
            alert(message);
            console.log("upload done "+ new Date());
    
        },
        error: function (e) {
            console.log("ERROR : ", e.responseText);
            alert("ERROR : "+  e.responseText);
    
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 2011-09-13
      • 2021-08-18
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多