0

I have a program (Spring Boot) which is using REST service to upload files to the server or any other given location. But when I use the same service below error happened and below is the problem.

I am getting 400() error without any description when uploading a file via REST service. This application is Spring boot application which use java-script front-end to upload and download files via implemented rest service.

Help to resolve this. your help is appreciated. Thanks.

error is : enter image description here

below is the code: 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 service:

@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);

}
Dilanka M
  • 372
  • 1
  • 5
  • 17

2 Answers2

2

I created the same scenario in my local environment with postman like this; enter image description here

Server side with one small change (@RequestMapping(value = "/upload", method = RequestMethod.POST));

@RequestMapping(value = "/upload", method = RequestMethod.POST)
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);
    }
    return new ResponseEntity("Successfully uploaded - " + uploadfile.getOriginalFilename(), new HttpHeaders(), HttpStatus.OK);
}

It works.

Habil
  • 540
  • 1
  • 8
  • 20
0

My assumption: your method saveUploadedFiles throws an IOException.

As you are getting a 400 Bad Request as response, I Think you should debug your

} catch (IOException e) { e.printStackTrace(); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); }

Block in order to find out what causes a IOException.

mrkernelpanic
  • 4,268
  • 4
  • 28
  • 52