无法使用Axios和FormData将文件上载到Spring引导REST

无法使用Axios和FormData将文件上载到Spring引导REST,spring,spring-mvc,vue.js,post,axios,Spring,Spring Mvc,Vue.js,Post,Axios,我尝试使用axios从页面上传文件,但无法在控制器上获取。 在fromt端,我将Vue.js与axios一起使用,并在Spring MVC控制器的后端使用。在spring中,我的控制器似乎无法将FormData()转换为MultipartFile。我读了很多问题,但没有答案。这是我的密码: <!DOCTYPE html> <html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head>

我尝试使用axios从页面上传文件,但无法在控制器上获取。 在fromt端,我将Vue.js与axios一起使用,并在Spring MVC控制器的后端使用。在spring中,我的控制器似乎无法将FormData()转换为MultipartFile。我读了很多问题,但没有答案。这是我的密码:

<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- Vue.js development version, includes helpful console warnings -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <!--Axios dependency-->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <!--Bootstrap-->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>

</head>
<body>
    <h4>Uploading files with Vue.js</h4>

    <div id="uploadSingle" class="container">

            <h5>Single file uploading</h5>
            <div class="large-12 medium-12 small-12 cell">

                <div class="form-row" >
                    <div class="col-md-4 mb-3">
                        <input type="file" ref="file" id="customFile"
                               v-on:change="handleFileUpload($event)"
                               class="custom-file-input"
                               enctype="multipart/form-data">
                        <label class="custom-file-label" for="customFile">{{chosenFile}}</label>
                    </div>
                </div>
                <button v-on:click="submitFile()" class="btn btn-primary">Submit</button>
            </div>
    </div>


    <div id="uploadMultiple" class="container">
        <h5>Multiple files uploading</h5>
    </div>


    <script >
        var app = new Vue({
            el: '#uploadSingle',
            data() {
                return {
                    message: 'Hello Vue!',
                    singleFile: '',
                    refFile: '',
                    chosenFile: 'Chose file'
                };
            },
            methods:{
                handleFileUpload(event){
                    this.singleFile = event.target.files[0];
                    this.chosenFile=this.singleFile.name;
                },
                submitFile(){
                    var formData = new FormData();
                    formData.append("file", this.singleFile);

                    axios.post( '/single-file',
                        formData,{
                            headers: {
                                'Content-Type': 'multipart/form-data'
                            }

                        }
                    ).then(function(){console.log('SUCCESS!')})
                        .catch((error) => console.log( error ) )

                },
            },
        })
    </script>
</body>
</html>

标题
使用Vue.js上载文件
单文件上传
{{chosenFile}}
提交
多文件上传
var app=新的Vue({
el:'上传单一',
数据(){
返回{
消息:“你好,Vue!”,
单个文件:“”,
引用文件:“”,
chosenFile:“选择文件”
};
},
方法:{
handleFileUpload(事件){
this.singleFile=event.target.files[0];
this.chosenFile=this.singleFile.name;
},
submitFile(){
var formData=new formData();
formData.append(“文件”,this.singleFile);
axios.post(“/single file”,
formData{
标题:{
“内容类型”:“多部分/表单数据”
}
}
).then(函数(){console.log('SUCCESS!')})
.catch((错误)=>console.log(错误))
},
},
})
和控制器文件:

package com.yurets_y.webdevelopment_uploading_file_with_vue.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@CrossOrigin("*")
@Controller
public class UploadController {

    @Value("${upload.path}")
    private String uploadPath;


    @ResponseBody
    @PostMapping(value="/single-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<?> uploadSingle(
            @RequestParam(name="file", required = false) MultipartFile file
    ) {
        System.out.println("uploaded");
        System.out.println(file);


        return ResponseEntity.ok().build();


    }


}
package com.yurets_y.webdevelopment_上传_文件_与_vue.controller;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.http.MediaType;
导入org.springframework.http.ResponseEntity;
导入org.springframework.stereotype.Controller;
导入org.springframework.web.bind.annotation.*;
导入org.springframework.web.multipart.MultipartFile;
导入java.io.IOException;
@交叉原点(“*”)
@控制器
公共类上载控制器{
@值(${upload.path}”)
私有字符串上传路径;
@应答器
@PostMapping(value=“/single file”,consumes=MediaType.MULTIPART\u FORM\u DATA\u value)
公共响应上传单(
@RequestParam(name=“file”,required=false)多部分文件
) {
System.out.println(“上传”);
System.out.println(文件);
返回ResponseEntity.ok().build();
}
}
我将非常感谢你的建议

附言。
当我使用@RequestParam(name=“file”,required=true)MultipartFile文件时,我收到错误POST 400(错误请求),看起来spring无法从FormData获取MultipartFile文件。在后端,我没有收到任何错误,只有两天后,我花了两天时间来解决我的问题,我找到了几个工作项目,比较了所有文件,发现我的问题是依赖性, 我使用了依赖关系:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

org.springframework.boot
弹簧启动器webflux
当我改变依赖关系时:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

org.springframework.boot
SpringBootStarterWeb
现在它正按照我需要的方式工作