【问题标题】:The request sent by the client was syntactically incorrect- when uploading a file客户端发送的请求在语法上不正确 - 上传文件时
【发布时间】:2013-01-12 22:53:41
【问题描述】:

当我尝试在春季上传文件时出现以下异常:

The request sent by the client was syntactically incorrect.

这就是我通过 ajax 提交值的方式:

function uploadComment(viewName,answers){
    var file_data = $("#CIMtrek_daily_originator_comments").val();
    alert("file_data "+file_data);
    $.ajax({
            type : "POST",
            url : "CIMtrek_Compliance_Daily_Shipments_FileUpload",
            data: {
                uploadFile : file_data,
                id : '0'
            },
        success: function (msg) {
            global.getElementById("CIMtrek_uploadedFileName").innerHTML=msg;
             alert("File Uploaded");
        }
    });

}

这是我的控制器方法:

@RequestMapping(value = "/CIMtrek_Compliance_Daily_Shipments_FileUpload", method = RequestMethod.POST)
    public String createComments(@RequestParam("uploadFile") MultipartFile  uploadItem ) {
        System.out.println(" CIMtrek_Compliance_Daily_Shipments_FileUpload : ");
        String uploadedFileName="";
        try {
            String fileName = null;
            InputStream inputStream = null;
            OutputStream outputStream = null;
            if (uploadItem.getSize() > 0) {
                inputStream = uploadItem.getInputStream();

                System.out.println("size::" + uploadItem.getSize());
                fileName = "/WEB-INF/resources/Attachment/"+ uploadItem.getOriginalFilename();

                outputStream = new FileOutputStream(fileName);
                System.out.println("fileName:" + uploadItem.getOriginalFilename());

                int readBytes = 0;
                byte[] buffer = new byte[10000];
                while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
                    outputStream.write(buffer, 0, readBytes);
                }
                outputStream.close();
                inputStream.close();
            }
            uploadedFileName =uploadItem.getOriginalFilename();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadedFileName;
    } 

请帮我解决这个问题。

【问题讨论】:

    标签: java spring jquery file-upload


    【解决方案1】:

    您需要使用 serialize() 而不是 val(),如下所示

    数据:$("#your_form").serialize();

    $.ajax({
    type : "POST",
    contentType:false,
    url : "CIMtrek_Compliance_Daily_Shipments_FileUpload",
    data: $("#your_form").serialize(),
        success: function (msg) {
            global.getElementById("CIMtrek_uploadedFileName").innerHTML=msg;
             alert("File Uploaded");
        }
    });
    

    【讨论】:

    • 很抱歉您的回答对我没有帮助,如果您不介意,您能否详细说明您的回答是在哪里放置此代码?
    • 不,CIMtrek_daily_originator_comments是html输入文件名。
    • 在控制器中您正在寻找“uploadFile”,您需要在此处使用输入文件名。如果 CIMtrek_daily_originator_cmets 是您的输入文件,请在控制器中使用它。您还添加了多部分解析器 bean 吗?
    • 我添加了multipart resolver bean并将输入file名称更改为CIMtrek_daily_originator_comments这是我现在得到的异常,错误:ReferenceError:attr未定义
    • 这是我的多部分解析器 bean
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2019-02-20
    • 2019-05-14
    • 2013-02-05
    相关资源
    最近更新 更多