java 当前请求不是多部分请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38294304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:17:50  来源:igfitidea点击:

The current request is not a multipart request

javaspring-mvcfile-uploadspring-boot

提问by Darshan Dalwadi

On uploading my file by ajax I am facing Exeption

通过ajax上传我的文件时,我面临Exeption

org.springframework.web.multipart.MultipartException: The current request is not a multipart request

org.springframework.web.multipart.MultipartException:当前请求不是多部分请求

I google this and find many solution,applied all of them no one resolved my problem-

我用谷歌搜索并找到了很多解决方案,应用了所有解决方案没有人解决我的问题-

Below is my html-file

下面是我的 html-file

<form id="upload-file-form">
   <label for="upload-file-input">Upload your file:</label>
   <input id="upload-file-input" type="file" name="uploadfile" accept="*" enctype="multipart/form-data" />
</form>

Script for ajax-

ajax脚本-

$.ajax({
         url: "/util/uploadFile",
         type: "POST",
         data: {'uploadfile':new FormData($("#upload-file-form")[0])},
         enctype: 'multipart/form-data',
         processData: false,
         contentType: false,
         cache: false,
         success: function () {},
         error: function () {}
         });

And this is my Spring boot Controller("/util")-

这是我的 Spring Boot Controller("/util")-

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    @ResponseBody
    public String uploadFile(@RequestParam("uploadfile") MultipartFile uploadfile) {
        System.out.println("----------------");
        System.out.println("----------------" + uploadfile);
        return "success";
    }


    @Bean
    public MultipartConfigElement multipartConfigElement() {
        return new MultipartConfigElement("");
    }

    @Bean
    public MultipartResolver multipartResolver() {
        org.springframework.web.multipart.commons.CommonsMultipartResolver multipartResolver = new org.springframework.web.multipart.commons.CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(4000000);
        return multipartResolver;
    }

回答by Darshan Dalwadi

You are sending an Ajax request and you are using name of input field directly in your controllerwhich cause the problem. Because when ajax request come to the controller it doesn't found any parameter with name "uploadfile"that's why it giving you error.

您正在发送 Ajax 请求并you are using name of input field directly in your controller导致问题。因为当 ajax 请求到达时,controller it doesn't found any parameter with name "uploadfile"这就是它给你错误的原因。

Here I've just put key for the file and it accept request. The code written below is work for me.

在这里,我刚刚为文件输入了密钥,它接受了请求。下面写的代码对我有用。

Ajax code,

阿贾克斯代码,

var formData = new FormData();
var file = $('#fileInputId')[0].files[0];

formData.append("myFileKey", file);

$.ajax({
        url : 'myUrl',
        type : 'POST',
        data : formData,
        enctype : 'multipart/form-data',
        contentType : false,
        cache : false,
        processData : false,
        success : function(response) {},
        error: function(){}    
     )};

Java Controller Code:,

Java 控制器代码:,

    @PostMapping(value = { "/myUrl" }, consumes = { "multipart/form-data" })
    public ModelAndView readFileData(@RequestParam("myFileKey") MultipartFile uploadedFile) throws IOException 
   {
                 Your Stuff...... 
   }