JAVA:上传分段文件

时间:2018-10-03 18:22:59

标签: java angularjs spring multipartform-data

我正在使用Spring控制器中的Multipart文件上载。我已经阅读了多个问题,谷歌,但是似乎没有任何作用。

我知道

error: "Bad Request"
exception: "org.springframework.web.multipart.support.MissingServletRequestPartException"
message: "Required request part 'file' is not present"

我的BE控制器:

@RequestMapping(value = "/zip", method = RequestMethod.POST)
public void readFile(@RequestParam("file") MultipartFile file) throws IOException { 
// code
}

FE,angularJS:

service.getZip = function getZip(file) {
    var formData = new FormData();
    formData.append('file', file);
    return $http({
        method: 'POST',
        url: CONSTANTS.readFile,
        data: formData,
        headers: {'Content-Type': undefined}
    }) .then(function (response) {
        var data = response.data;
        return data.id;
    });
}

HTML:

<input type="file" id="file" name="file" accept=".txt"/>

application.properties还包含:

spring.http.multipart.enabled=false

更新:

遵循@ Byeon0gam建议从控制器中删除@RequestParam时,我不再遇到该错误,但是我的文件在控制器中为空。尽管在FE服务中,如我所见,它并不为空:

enter image description here

1 个答案:

答案 0 :(得分:0)

将您的FE中的Content-Type更改为:

headers: {'Content-Type': 'x-www-form-urlencoded'}

希望它对您有用。

相关问题