'Required request part 'file' is not present]

I need to upload a file to a folder ( with giving path )

but i'm getting this error when invoking my conrtoller usinf postman ( key = file , value (myfile))

This is my controller :

@RestController
@RequestMapping(value = "/upload")
public class FileUploadController {
    @Autowired
    FileUploadService fileUploadService ;
    
    
    @PostMapping(value = "/uploadprometheusconfig")
    public void uploadFile(@RequestPart(name ="file",required=true) MultipartFile file ) {
        try {
            fileUploadService.uploadfile(file);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

and this is my service :

@Service
public class FileUploadService {
    
    public void uploadfile(MultipartFile file) throws IllegalStateException, IOException {
        file.transferTo(new File("C:\\Users\\21620\\Desktop\\prometheusconfig" + file.getOriginalFilename()));
    }
}

and this is a screenshot from postman :

enter image description here

can anyone please help ?



Solution 1:[1]

The request parameter you submitted did not specify the key. You should specify the key when submitting the request in postman. This value should be the same as the value of name in @RequestPart. In your example, key should be file. like this: enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cli ash