簡體   English   中英

JAVA:上傳分段文件

[英]JAVA: Upload Multipart file

我在Spring控制器中嘗試分段文件上傳。 我已經閱讀了多個問題,谷歌,但似乎沒有任何作用。

我懂了

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時,我不再遇到該錯誤,但是由於涉及控制器,我的文件為null。 雖然在FE服務中,如我所見,它並不為空:

在此處輸入圖片說明

將您的FE中的Content-Type更改為:

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

希望它對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM