Тёмный

Spring Boot File Upload and Download REST API Examples 

Code Java
Подписаться 50 тыс.
Просмотров 48 тыс.
50% 1

In this video, I'd love to share with you how to develop REST APIs for file upload and download with Spring Boot. With the uploaded files are stored on disk; and random codes are used to identify files in download API.
Technologies:
- Spring Boot with Spring Web MVC, Spring RESTful Web Services
- Maven build
Software programs:
- Java Development Kit (OpenJDK / Oracle JDK)
- Spring Tool Suite IDE (STS)
- Postman for testing REST APIs
You can use the code examples in the written article at www.codejava.net/frameworks/s...
Recommended Spring Boot REST APIs Ultimate Course: www.udemy.com/course/spring-b... - Hands-on REST API Development with Spring Boot: Design, Implement, Document, Secure, Test, Consume RESTful APIs.

Наука

Опубликовано:

 

2 мар 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 92   
@CodeJava
@CodeJava 2 года назад
To get code examples in this video, visit www.codejava.net/frameworks/spring-boot/file-download-upload-rest-api-examples
@ramosespann6496
@ramosespann6496 2 года назад
your REST API in Spring Boot videos are great
@CodeJava
@CodeJava 2 года назад
Glad you like them!
@riyarohan311z
@riyarohan311z 11 месяцев назад
Thank you, very informative video. This helped lots with my internship assignment.
@CodeJava
@CodeJava 11 месяцев назад
Glad it was helpful!
@javadeveloper1383
@javadeveloper1383 2 года назад
Best video for Upload&Download File REST API! THANKS!!!
@CodeJava
@CodeJava 2 года назад
Thanks and welcome
@xuaniennguyen6573
@xuaniennguyen6573 2 года назад
Hi anh, em có thắc mắc muốn hỏi là trong trường hợp download.Thay vì trả về đối tượng Resource như video thì có trả về 1 mảng byte thì kết quả cũng tương tự. Anh cho em hỏi là có sự khác nhau của việc dùng Resource và mảng byte đối với chức năng download ko ạ ? Và em muốn hỏi thêm anh là best practice khi download file (tải được file nặng không bị tràn bộ nhớ) là gì ạ? Cảm ơn anh !
@CodeJava
@CodeJava 2 года назад
theo giao thức HTTP thì cuối cũng vẫn là gửi byte dữ liệu cho client thôi. Nên về bản chất không có gì khác cả. Còn đối với tải file nặng thì cần đọc từng chút dữ liệu từ file ra và ghi xuống response. Dùng mảng buffer byte[], sau khi viết xuống response thì clear mảng này đi. Làm như thế thì không bị tràn bộ nhớ.
@xuaniennguyen6573
@xuaniennguyen6573 2 года назад
@@CodeJava Ở video trên khi lấy file từ máy tính em thấy anh dùng foreach, tại sao a dùng foreach thay vì get() vậy ạ ? Nếu file trong thư mục lưu trữ nhiều thì em nghĩ nó sẽ tốn nhiều thời gian hơn khi dùng get() đúng ko a ? Vị dụ : Path path = Paths.get(fileStorageLocation).toAbsolutePath().resolve(fileName);
@tural.ahmatov
@tural.ahmatov Год назад
Thank you. Nice work there!
@CodeJava
@CodeJava Год назад
Glad you liked it!
@ChinhNguyen-eg5km
@ChinhNguyen-eg5km 2 года назад
Video rất bổ ích ạ, mong anh làm 1 video về roadmap của Spring Boot ạ
@CodeJava
@CodeJava 2 года назад
ok em.
@congdatt
@congdatt Год назад
@@CodeJava vẫn chưa có a ạ ?
@Manikandan-nn2bw
@Manikandan-nn2bw Год назад
We could've used UUID right? Does RandomString method always return unique strings?
@CodeJava
@CodeJava Год назад
it's not 100% guarantee. You can add unique constraint for the column so if it throws constraint violation exception that means a duplicate string was generated. But I think the possibility is very low.
@ktn4
@ktn4 2 года назад
Building api server with spring is the best, now let's build UI with modern frontend framework 😁🦊
@CodeJava
@CodeJava 2 года назад
Agree. I will have such videos soon.
@magadiflo-dev
@magadiflo-dev Год назад
This is great, thank you
@CodeJava
@CodeJava Год назад
You're very welcome!
@manit1184
@manit1184 Год назад
thưa thầy sửa lỗi này thế nào ạ?" The Unicode character [ư] at code point [432] cannot be encoded as it is outside the permitted range of 0 to 255"
@anarmammadzada476
@anarmammadzada476 Год назад
Very helpful. Thanks sir...
@CodeJava
@CodeJava Год назад
Happy to help
@abishasherlin5896
@abishasherlin5896 9 месяцев назад
I need to create career portal where userdetails along with resume upload is there.. They asked me to do like save the file in directory and filename in db with same entity class... . Can I do like this?
@CodeJava
@CodeJava 9 месяцев назад
sure, you can. That's a good practice. I usually implement that way.
@muskanburla9618
@muskanburla9618 Год назад
Thanks it helped me a lot
@CodeJava
@CodeJava Год назад
Glad it helped
@marouenghozzi4778
@marouenghozzi4778 2 года назад
Hello , i'm getting an error in the upload " required request part file is not present , all my code is similar as yours and postman also
@CodeJava
@CodeJava 2 года назад
You should put a key named "file" in the request's body. Check the written article here: www.codejava.net/frameworks/spring-boot/file-download-upload-rest-api-examples
@thanhnam.thai2811
@thanhnam.thai2811 2 года назад
Chào anh, em có thắc mắc là nếu ta có 2 files với fileCode giống nhau thì sao ạ?
@CodeJava
@CodeJava 2 года назад
thì file đầu tiên tìm thấy sẽ được download nhé. Nhưng mà xác suất 2 random code giống nhau là rất thấp.
@vishruthreddy7078
@vishruthreddy7078 Год назад
Hi,Can you also share the headers as i am getting this error The server does not support the request payload’s media type
@CodeJava
@CodeJava Год назад
Kindly check all the code here: www.codejava.net/frameworks/spring-boot/file-download-upload-rest-api-examples
@eyamejri5409
@eyamejri5409 Год назад
i added the annotatin @document to the entity but still not saving the data in my db , I use mongo db compass any solution
@CodeJava
@CodeJava Год назад
did you follow this guide? spring.io/guides/gs/accessing-data-mongodb/
@calintamas7576
@calintamas7576 2 года назад
Thanks! It is possible a repo in git?
@CodeJava
@CodeJava 2 года назад
No, there is not. Kindly follow the video.
@taregbalola
@taregbalola 2 года назад
It's great 👍 Can you make login and registration api with spring security
@CodeJava
@CodeJava 2 года назад
I will try. Thanks.
@jasnajacob2639
@jasnajacob2639 Год назад
Thank you sir❤
@CodeJava
@CodeJava Год назад
Welcome!
@pepepriest5973
@pepepriest5973 Год назад
Where these files upload when its deployed? If you deploy it as jar or war
@CodeJava
@CodeJava Год назад
It depends where you decide to save the files in your code. It can be relative to the app directory.
@jackielee3332
@jackielee3332 Год назад
thank you, thumb up!
@CodeJava
@CodeJava Год назад
Welcome!
@amenijouini8143
@amenijouini8143 2 года назад
thank u how can i save it in database?
@CodeJava
@CodeJava 2 года назад
check this video: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ryRQ6qXLLYM.html
@chandrasekar2471
@chandrasekar2471 Год назад
Im getting "errorMessage": "Exception : 400 BAD_REQUEST \"Required MultipartFile parameter 'file' is not present\"" any help will be appricated
@CodeJava
@CodeJava Год назад
Are you testing using Postman? Make sure you put a parameter named file in the request body. Check out the written article here: www.codejava.net/frameworks/spring-boot/file-download-upload-rest-api-examples
@alessandrocappiello112
@alessandrocappiello112 2 года назад
hi, great tutorial, could you link a github repo to this project? thanks a lot
@CodeJava
@CodeJava 2 года назад
There's no code repo. Kindly follow the video.
@alessandrocappiello112
@alessandrocappiello112 2 года назад
@@CodeJava Thanks anyway. I have a question, how should i go about uploading files on a mysql database instead of a directory? I tried changing the upload path to the url of my database but i had no success. Thanks again for the wonderful tutorial :)
@CodeJava
@CodeJava 2 года назад
@@alessandrocappiello112 reference: www.codejava.net/coding/upload-files-to-database-with-spring-mvc-and-hibernate
@hoanle2320
@hoanle2320 3 месяца назад
nice video
@CodeJava
@CodeJava 3 месяца назад
Thanks!
@namnguyen-il9tf
@namnguyen-il9tf 2 года назад
anh làm ví dụ về upload file rest api sử dụng base 64 được không ạ ?
@CodeJava
@CodeJava 2 года назад
Okay, I will try.
@namnguyen-il9tf
@namnguyen-il9tf 2 года назад
@@CodeJava appreciate it a lot
@rajnikant9123
@rajnikant9123 Год назад
Hello Sir !! How we can insert DATA and IMAGE both together into Database in ONE ROW of database using spring boot: Like:- private int id, private String firstName, private String lastName, private String email, private long mobileNumber, private String email, private byte [ ] image, please help me!!
@CodeJava
@CodeJava Год назад
you need to read an array of byte from the file uploaded (via MultipartRequest) and set it to the entity object before persisting. Check this video: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-ryRQ6qXLLYM.html
@danielfp7880
@danielfp7880 8 месяцев назад
Thank you
@CodeJava
@CodeJava 8 месяцев назад
You're welcome :)
@12_adesh_mahto_csbs66
@12_adesh_mahto_csbs66 Год назад
File not found coming what I can do?
@CodeJava
@CodeJava Год назад
check the file path.
@naveen7752
@naveen7752 11 месяцев назад
What about pdf upload inspite of image
@CodeJava
@CodeJava 11 месяцев назад
PDF or image are simply binary files. So it is the same.
@timothystone
@timothystone 4 месяца назад
@20:00 I think the use of a UUID4 would have been better here. No need for Commons (though I love Commons), just use Java.
@CodeJava
@CodeJava 4 месяца назад
That's good also.
@timothystone
@timothystone 4 месяца назад
On File Upload the HTTP response should be 201 Created with a Location header to the new resource. 200 OK (or Found) @13:31 is arguably not semantic.
@CodeJava
@CodeJava 4 месяца назад
you're right. That makes sense.
@lagoawb
@lagoawb Год назад
Very Niceeeeeeeee
@CodeJava
@CodeJava Год назад
Thank you! Cheers!
@ajitkumarneeraj9374
@ajitkumarneeraj9374 2 года назад
Sir please make video on how to use jwt token in spring security, how to login with jwt and logout .
@CodeJava
@CodeJava 2 года назад
Sure, I will try. Thanks for your suggestion.
@javadeveloper1383
@javadeveloper1383 2 года назад
@@CodeJava thank you! :)
@abchowdary9163
@abchowdary9163 2 года назад
Good
@CodeJava
@CodeJava 2 года назад
Thanks!
@congdatt
@congdatt Год назад
import org.apache.commons.lang3.RandomStringUtils; is not working =))
@CodeJava
@CodeJava Год назад
check for alternative solutions in this article: codejava.net/coding/generate-random-strings-examples
@congdatt
@congdatt Год назад
@@CodeJava omg.Thanks bro a lot
@vicary__
@vicary__ Год назад
I don't even know what language you're speaking man
@CodeJava
@CodeJava Год назад
sorry to hear that. so check my written tutorial instead: www.codejava.net/frameworks/spring-boot/file-download-upload-rest-api-examples
@LeonardoFernandezDuarte
@LeonardoFernandezDuarte 2 года назад
I have a internal error { "timestamp": "2022-04-14T02:42:25.578+00:00", "status": 500, "error": "Internal Server Error", "trace": "java.lang.NullPointerException ", "message": "No message available", "path": "/download/e0Yedr7E" }
@CodeJava
@CodeJava 2 года назад
check the exception printed in the console - that error means something is null.
@LeonardoFernandezDuarte
@LeonardoFernandezDuarte 2 года назад
@@CodeJava Sorry fail me, de path variable in my controller are incorrect, fix this it and working perfect
@Penta_Pinguino
@Penta_Pinguino Год назад
Not working for me. When I try to upload file with postman, I received: "timestamp": "2023-01-24T14:05:27.090+00:00", "status": 500, "error": "Internal Server Error", "trace": "Could not save file: name.png at net.codejava.api.upload.FileUploadUtil.saveFile(FileUploadUtil.java:25) ~[classes/:na] at net.codejava.api.upload.FileUploadController.uploadFile(FileUploadController.java:23) ~[classes/:na] ..." (and so on) On Spring Suite I received: "Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception" If usefull Spring Boot Version: 3.0.2 What is the issue ? I'm a newbie. Thank you
@Penta_Pinguino
@Penta_Pinguino Год назад
Maybe it's the Spring Boot Version. I change it to 2.7.8 and now it's work
@CodeJava
@CodeJava Год назад
I see. Glad to hear that it works.
@carlosalexandredev21121
@carlosalexandredev21121 Год назад
Thank you
@CodeJava
@CodeJava Год назад
You're welcome
Далее
File Upload and Download with Spring Boot - REST API
29:04
Spring Controller Based Exception Handler Examples
26:42
Building REST APIs with Spring Boot
1:16:44
Просмотров 86 тыс.