MissingServletRequestPartException:使用Ajax不存在必需的请求零件文件

时间:2019-02-11 12:55:59

标签: java ajax file spring-boot multipartform-data

我尝试了太多不同的方法,但我的错误仍然相同,无法从我的角度解决。其显示

  

由处理程序执行引起的已解决异常:org.springframework.web.multipart.support.MissingServletRequestPartException:所需的请求部件文件不存在

我的jsp代码是

<form name="upload_document_form" onsubmit="return false" enctype="multipart/form-data">
<input type="file" name="file"/>
<button class="btn btn-primary btn-lg" name="upload_document_form_btn" id="upload_document_form_btn" onclick="UploadDocuments()">Upload</button>
</form>

Ajax调用功能

function UploadDocuments(){
    var formData = new FormData();
        formData.append('file',$("#file").val());  

    $.ajax({
        type: 'POST',
        url: 'http://localhost:8080/insertDocumentData',
        enctype: 'multipart/form-data',
        data: formData,
        type: 'POST',
        dataType:'json',
        contentType: false,
        processData: false,
        success: function(msg) {

        }
    });
}

控制器是

private static String UPLOAD_FOLDER = "uploaded_Doc/AP12345";

@PostMapping("/insertDocumentData")
public boolean insertDocumentData(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) throws IOException{


    if (file.isEmpty()) {
        System.out.println("file is empty");
        return false;
    }

    try {
        byte[] bytes = file.getBytes();
        Path path = Paths.get(UPLOAD_FOLDER +"/"+   file.getOriginalFilename());
        Files.write(path, bytes);

    } catch (IOException e) {
        e.printStackTrace();
    }

    return  true;
 }

0 个答案:

没有答案