使用jquery-ajax在spring中无法使用文件上传

时间:2013-01-29 08:29:33

标签: spring jquery spring-mvc file-upload

这是我的表格:

<form name="CIMtrek_Compliance_Daily_Shipments" enctype="multipart/form-data">
<input type="file" id="CIMtrek_comments" name="CIMtrek_comments" value="" />
    <button id="upload" onclick="uploadCommentFile()">Upload</button>
</form>

这是我使用jquery的ajax调用:

function uploadCommentFile(){
    $("#upload").live("click", function() {
        var file_data = $("#CIMtrek_comments").prop("files")[0];   // Getting the properties of file from file field
        var form_data = new FormData();                  // Creating object of FormData class
        form_data.append("file", file_data)              // Appending parameter named file with properties of file_field to form_data
        //form_data.append("user_id", 123)                 // Adding extra parameters to form_data
        $.ajax({
                    type: 'POST',
                    url: "/CIMtrek_Compliance_Daily_Shipments_FileUpload",
                    dataType: 'script',
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: {
                         uploadFile:  file_data
                       },
                    success: function (msg) {
                        global.getElementById("CIMtrek_uploadedFileName").innerHTML=msg;
                    }
           })
    })

}

这是我的弹簧控制器:

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

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

                System.out.println("path : "+request.getRealPath("") + "/WEB-INF/resources/Attachment");
                outputStream = new FileOutputStream(fileName);
                System.out.println("fileName:" + file.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 =file.getOriginalFilename();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uploadedFileName;
    }

但是当我点击上传按钮时,我得到以下异常:

HTTP Status 400 - 
The request sent by the client was syntactically incorrect.

可能是什么问题,请帮我识别。

最诚挚的问候。

1 个答案:

答案 0 :(得分:1)

关注this帮助解决我的问题: