4

I am working on a file upload controller and I am currently getting the following error when testing in Postman.

{
    "timestamp": "2019-04-18T14:53:07.988+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "Required request part 'file' is not present",
    "path": "/upload"
}

At the moment my controller is very simple but first I need to overcome this problem.

I have looked at the answers given [here](upload file springboot Required request part 'file' is not present"upload file springboot Required request part file is not present")!

But unfortunately, anything suggested here did not resolve my problem

Any help with this error would be appreciated

This is my controller:

@Controller
public class UploadController {

@ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public boolean upload(@RequestParam("file") MultipartFile file) throws IOException {

        try {
            if (!file.isEmpty()) {
                return true;
            } else {
                return false;
            }
        }
        catch(Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
Stephen
  • 41
  • 1
  • 5
  • first remove **produces = MediaType.IMAGE_JPEG_VALUE**, it means the return of this request will a jpeg or image file . And how are you sending file from postman .can you share it – Muhammad Ahmed Apr 18 '19 at 15:22
  • @Muhammad Under 'Body' I have form data checked and then my value is my jpg file I'm trying to send. I have nothing checked under headers or anywhere else – Stephen Apr 18 '19 at 15:34
  • the key of the file should be "file" . for more understanding see this site https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/ – Muhammad Ahmed Apr 18 '19 at 15:39
  • @Muhammed Yes File is already set as the key – Stephen Apr 18 '19 at 15:40

3 Answers3

0

It's difficult without knowing how you are sending the data but here's how i solved sending multipart/form-data through a @RestController:

@PostMapping(value = "/foo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
ResponseEntity fileUpload(@Requestparam("bar") LinkedList<MultipartFile> payload) {
    MultipartFile file = payload.get(0)
    ...

Spring just wouldn't accept anything another than a linked list in my case, but that was form-data sent as an Angular2+ FormData object with field name bar.

Marcus Grass
  • 1,043
  • 2
  • 17
  • 38
0

As you hasve not mentioned your request model, let it be EarningRequest, so know your model data is:

class EarningRequest{
   private FilePart file;
   //and other data which you want to add.
   //add the getter setters also, so that jackson can map the json to this pojo
}

@RestController
public class UploadController {

    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public boolean upload (@ModelAttribute EarningRequest earningRequest){ 
         //earningRequest contains the file
         //you can get the filePart as earningRequest.getFile()
         return true;
    }

}

Bishal Jaiswal
  • 1,684
  • 13
  • 15
0

In postman under "key" I wasn't setting anything. I needed to set this as 'file'. I previously made the assumption all I had to do was click the drop-down and select file.

I will include below all the updated code & a link to the image which explains this better(I couldn't display image here as reputation < 10)

link to postman Image

@RestController
public class UploadController {

    @PostMapping("/upload")
    @ResponseBody
    public boolean upload(@RequestParam("file") MultipartFile file) {
        try{
            if(file.isEmpty() ==false){
                System.out.println("Successfully Uploaded: "+ file.getOriginalFilename());

                return true;
            }
            else{
                System.out.println("ERROR");
                return false;
            }
        }
        catch(Exception e){
            System.out.println(e);
            return false;
        }
    }
}
Stephen
  • 41
  • 1
  • 5