當通過REST服務上傳文件時,我得到400()錯誤,沒有任何描述。此應用程序是Spring啓動應用程序,它使用java-script前端通過實施的休息服務上傳和下載文件。400()錯誤,REST文件上傳錯誤
幫助解決這個問題。你的幫助表示讚賞。謝謝。
下面是代碼: JS:
document.getElementById('import2').onclick = function() {
var files = document.getElementById('selectFiles').files;
console.log(files);
if (files.length <= 0) {
return false;
}
//serverUploaded = true;
var form_data = new FormData(files.item(0));
//from new NLP
$.ajax({
type: "POST",
url: "http://localhost:8080/gsta/upload",
processData: false,
contentType: false,
async: false,
cache: false,
data: form_data,
success: function (result) {
//alert(JSON.stringify(result));
//$("#out_lexalytics").html(JSON.stringify(result));
if (result) {
if(result.statusCode == 200)
{
serverUploaded = true;
}
}
}
});
}
REST服務:
@PostMapping("/upload")
// If not @RestController, uncomment this
@ResponseBody
public ResponseEntity<?> uploadFile(@RequestParam("data") MultipartFile uploadfile) {
logger.debug("Single file upload!");
if (uploadfile != null && uploadfile.isEmpty()) {
return new ResponseEntity("please select a file!", HttpStatus.OK);
}
try {
saveUploadedFiles(Arrays.asList(uploadfile));
} catch (IOException e) {
e.printStackTrace();
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity("Successfully uploaded - " + uploadfile.getOriginalFilename(), new HttpHeaders(), HttpStatus.OK);
}
你是正確的時候使用郵遞員休息客戶端,但你有沒有嘗試AJAX調用? – DMM
好的,勾選這個:[link](https://www.mkyong.com/spring-boot/spring-boot-file-upload-example-ajax-and-rest/) – Habil