spring boot file not found error
Tags: springboot, java, postman, webdev
Published: 2023-09-24 08:58:56

I was trying to upload image to my database through my spring boot app 
//This was my code to handle the image and data
public ResponseEntity<String> addProduct( Product product, MultipartFile file) {

        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();

        String userName= authentication.getName();
        Users admin = userRepository.findByUsername(userName);

        try {
            if (!file.isEmpty()) {
                byte[] imageData = file.getBytes();
                product.setImage(imageData);
                productRepository.save(product);
                ProductOwnerShip productOwnerShip=new ProductOwnerShip();
                productOwnerShip.setProduct(product);
                productOwnerShip.setUser(admin);
                productOwnerShipRepository.save(productOwnerShip);
            } else {
                return new ResponseEntity<>("Error: No image file uploaded", HttpStatus.BAD_REQUEST);
            }

            // Process and save the product with the image in your database



            return new ResponseEntity<>("Product added successfully", HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>("Error uploading image", HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }


//error from spring boot
2023-09-24 14:21:38.387  WARN 10344 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors<EOL>Field error in object 'product' on field 'image': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@52f1e52b]; codes [typeMismatch.product.image,typeMismatch.image,typeMismatch.[B,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [product.image,image]; arguments []; default message [image]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'byte[]' for property 'image'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'byte' for property 'image[0]': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile']]
2023-09-24 14:21:38.488 ERROR 10344 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] threw exception

java.io.FileNotFoundException: C:\Users\aryan\AppData\Local\Temp\tomcat.8080.15562901959233805749\work\Tomcat\localhost\ROOT\upload_72b1efba_2385_4ffe_9115_ee9129d61d39_00000020.tmp (The system cannot find the file specified)
	at java.base/java.io.FileInputStream.open0(Native Method) ~[na:na]
	at java.base/java.io.FileInputStream.open(FileInputStream.java:219) ~[na:na]
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:158) ~[na:na]

plz someone help me I am new to spring boot