尝试在springboot中将文件保存为可选文件

km0tfn4u  于 2021-08-25  发布在  Java
关注(0)|答案(2)|浏览(328)

我正在尝试将一个文件保存为springboot中的可选文件,其中包含额外的数据。因此,用户应该可以选择添加图像或不添加图像。当没有图像时,我收到一个无值错误。当有图像时,一切都保存得很好。

@RequestMapping(value = "/updateCustomer", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponseEntity<?> updateCustomer(@RequestPart("customer") @Valid Customer customer, @RequestPart("file") @Valid Optional<MultipartFile> image) throws IOException {

        byte[] imageData = null;
        if (image.isPresent() && image.get() != null)
            imageData = image.get().getBytes();
        if (imageData == null && customer.getId() != null) {
            Optional<Customer> readCustomer = customerRepository.findById(customer.getId());
            if (readCustomer.get() != null)
                imageData = readCustomer.get().getImage().getData();
        }
        if (imageData != null) {
            customer.setImage(new Binary(BsonBinarySubType.BINARY, imageData));
        }

        Customer result = customerRepository.save(customer);
        return ResponseEntity.ok().body(result);
    }

用于控制的模型

public class Customer {

    @Id
    private String id;
    private String username;
    private String name;
    private String surname;
    private String dob;
    private String position;
    private String email;
    private String contactNo;
    private String status;
    private Integer notificationValue;
    private Address address;
    private BusinessInformation businessInformation;
    private Binary image;
    private List<UserRolls> userRolls;
    private List<CustomerITMModules> entityITMModules;

我犯了一个错误

java.lang.NullPointerException: Cannot invoke "org.bson.types.Binary.getData()" because the return value of "com.mqa.modules.Admin.mst_Entity.models.Customer.getImage()" is null
bwitn5fc

bwitn5fc1#

所以采纳了大家的建议,提前感谢。已更改为控制器的可选映像。不是最有效的,但它很有效

@RequestMapping(value = "/updateCustomer", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ResponseEntity<?> updateCustomer(@RequestPart("customer") @Valid Customer customer,
                                            @RequestParam(name="file", required=false) MultipartFile image) throws IOException {

//Create New User
if(customer.getId() == ""){
    System.out.println("New User");
}else if(customer != null  ){  //On Edit User
    Customer user = this.customerRepository.findUsersByEmail(customer.getEmail());
    if(image == null && user.getImage() != null){  // If User has image save same image
        byte[] existingImage = user.getImage().getData();
        customer.setImage(new Binary(BsonBinarySubType.BINARY, existingImage));
        System.out.println(customer.getName());
    }

}
        //Save New Image
        if(customer.getId() != null  && image!= null){
            System.out.println("TestNew");
            byte[] newImage = image.getBytes();
            customer.setImage(new Binary(BsonBinarySubType.BINARY, newImage));
        }

        Customer result = customerRepository.save(customer);
        return ResponseEntity.ok().body(result);
    }
6ovsh4lw

6ovsh4lw2#

如果您也共享错误的堆栈跟踪,通常会很有帮助。
似乎只有在请求中存在相应的requestpart时,框架才能创建可选的。所以问题是,您使用可选参数作为方法参数的原因是什么?
您在代码中进行的所有检查都很难从可选类型的参数中获益。
在我看来,您应该重新构造代码,以简单地检查“image”参数是否为null,并相应地执行操作。
此外,方法参数“image”上的@valid注解可能是不必要的。只有对customer类本身进行了相应的注解时,“customer”方法参数的@valid注解才有意义。

相关问题