0

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

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;
    } 

请帮我解决这个问题。

4

1 回答 1

0

您需要使用 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");
    }
});
于 2013-01-29T11:30:45.360 回答