DEV Community

Aryan Dwivedi
Aryan Dwivedi

Posted on

spring boot file not found error

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 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);
    }

}
Enter fullscreen mode Exit fullscreen mode

//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 errorsField 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.(FileInputStream.java:158) ~[na:na]

plz someone help me I am new to spring boot

Top comments (17)

Collapse
 
schwiftycold profile image
Shubham Kumar

Can you share your application.properties file located inside the resources directory?

Collapse
 
rnd2002 profile image
Aryan Dwivedi

spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/odopdb
spring.datasource.username=root
spring.datasource.password=aryan007@mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql: true

spring.servlet.multipart.enabled=true

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.location=C:/Windows/Temp

Collapse
 
schwiftycold profile image
Shubham Kumar

The application.properties file seems fine to me.
Can you check the implementation of setImage method of the Product class?
I think there is some mismatch b/w the file type conversion.
Would it be possible for you to share the project codebase, maybe using version control repository?

Thread Thread
 
rnd2002 profile image
Aryan Dwivedi

Yes how can I reach you?

Thread Thread
 
rnd2002 profile image
Aryan Dwivedi
Thread Thread
 
rnd2002 profile image
Aryan Dwivedi

Hope it helps
I wanted a dashboard for the sellers and a product database so that I can display products to the customers and also implement dashboard facility to each sellers with basic auth for sellers..
Now hope it helps you to understand the problem

Thread Thread
 
schwiftycold profile image
Shubham Kumar

You can DM me on Twitter (now X) or you can also add me to your GitHub projects.

Github username: UnresolvedCold
Twitter Handle: SchwiftyCold

Thread Thread
 
schwiftycold profile image
Shubham Kumar

@rnd2002 the repository you shared is showing as empty.
Please notify me once you upload your code there.
Thanks

Thread Thread
 
rnd2002 profile image
Aryan Dwivedi

👍 done sir

Thread Thread
 
schwiftycold profile image
Shubham Kumar

Yeah I got it.
Will check and ping you once I find the problem.

Thread Thread
 
rnd2002 profile image
Aryan Dwivedi

Thanks in advance

Thread Thread
 
schwiftycold profile image
Shubham Kumar • Edited

Sorry for a late reply. I was occupied in some work. I have created a PR with some changes.

The problem was with the prioritization of "image" request parameter. It was defined in Product as a field and also as a request param.

Springboot was trying to bind the incoming multipart image file to product's byte array and hence producing the error.

The simple resolution is to use some other name for your request param. I have changed it to "data".

Thread Thread
 
rnd2002 profile image
Aryan Dwivedi • Edited

thanks for taking some time to review my code but problem still persists
and same error is producer
Image description
image that i have provided you has post request again thanks for your time
Image description

Thread Thread
 
schwiftycold profile image
Shubham Kumar

While sending the request, change the image field to data.

Thread Thread
 
schwiftycold profile image
Shubham Kumar

Image description

Thread Thread
 
rnd2002 profile image
Aryan Dwivedi

Thanks a lot ...I was stuck in this since 3 days and I thought of writing all the logic again but you saved my efforts 👍👌

Collapse
 
rnd2002 profile image
Aryan Dwivedi

thanks for giving some time to my question