简体   繁体   中英

How to send application/json data along with file in postman multipart/form-data post request?

My requirement is something like this. I have a DTO class as below

public class Employee{
    private Long id;
    private String name;
    private String designation;
    private byte[] employeeImage;
}

My API is below,

@PostMapping(value="/createEmployees")
            public ResponseEntity<List<EmployeeDTO>> createEmployees(@RequestParam("id") Long id, 
                    @RequestBody List<EmployeeDTO> employeeList){
}

I am trying to send a request using postman, but the image isn't getting saved. Below is my postman request.

邮递员请求

Everything is fine, but image isn't getting saved.

Anyhelp is greatly appreciated.

Thanks in advance!

If you are sending Base 64 encoded image, then also decode it some thing like this:

        //This will decode the String which is encoded by using Base64 class
        byte[] imageByte=Base64.decodeBase64(imageByteValue);

        String directory=servletContext.getRealPath("/")+"images/sample.jpg";

        new FileOutputStream(directory).write(imageByte);
        return "success ";

You should take your image from Employee DTO and decode it to save at its respective directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM