diff --git a/persistence-modules/spring-boot-persistence-mongodb/pom.xml b/persistence-modules/spring-boot-persistence-mongodb/pom.xml
index 585e54bf57..dae5efa6d0 100644
--- a/persistence-modules/spring-boot-persistence-mongodb/pom.xml
+++ b/persistence-modules/spring-boot-persistence-mongodb/pom.xml
@@ -20,6 +20,10 @@
org.springframework.boot
spring-boot-starter-web
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
org.springframework.boot
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/daos/PhotoRepository.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/daos/PhotoRepository.java
new file mode 100644
index 0000000000..d38e11c055
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/daos/PhotoRepository.java
@@ -0,0 +1,9 @@
+package com.baeldung.mongodb.daos;
+
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import com.baeldung.mongodb.models.Photo;
+
+public interface PhotoRepository extends MongoRepository {
+
+}
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Photo.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Photo.java
new file mode 100644
index 0000000000..13f1a3cd19
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Photo.java
@@ -0,0 +1,51 @@
+package com.baeldung.mongodb.models;
+
+import org.bson.types.Binary;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+@Document(collection = "photos")
+public class Photo {
+
+ @Id
+ private String id;
+
+ private String title;
+
+ private Binary image;
+
+ public Photo(String title) {
+ super();
+ this.title = title;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(String id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public Binary getImage() {
+ return image;
+ }
+
+ public void setImage(Binary image) {
+ this.image = image;
+ }
+
+ @Override
+ public String toString() {
+ return "Photo [id=" + id + ", title=" + title + ", image=" + image + "]";
+ }
+
+}
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Video.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Video.java
new file mode 100644
index 0000000000..617f0cdbfd
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Video.java
@@ -0,0 +1,39 @@
+package com.baeldung.mongodb.models;
+
+import java.io.InputStream;
+
+public class Video {
+ private String title;
+ private InputStream stream;
+
+ public Video() {
+ super();
+ }
+
+ public Video(String title) {
+ super();
+ this.title = title;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public InputStream getStream() {
+ return stream;
+ }
+
+ public void setStream(InputStream stream) {
+ this.stream = stream;
+ }
+
+ @Override
+ public String toString() {
+ return "Video [title=" + title + "]";
+ }
+
+}
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java
new file mode 100644
index 0000000000..685d67c40f
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java
@@ -0,0 +1,38 @@
+package com.baeldung.mongodb.services;
+
+import java.io.IOException;
+import java.util.Optional;
+
+import org.bson.BsonBinarySubType;
+import org.bson.types.Binary;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.baeldung.mongodb.daos.PhotoRepository;
+import com.baeldung.mongodb.models.Photo;
+
+@Service
+public class PhotoService {
+
+ @Autowired
+ private PhotoRepository photoRepo;
+
+ public Photo getPhoto(String id) {
+ Optional result = photoRepo.findById(id);
+ return result.isPresent() ? result.get() : null;
+ }
+
+ public String addPhoto(String title, MultipartFile file) {
+ String id = null;
+ try {
+ Photo photo = new Photo(title);
+ photo.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes()));
+ photo = photoRepo.insert(photo);
+ id = photo.getId();
+ } catch (IOException e) {
+ return null;
+ }
+ return id;
+ }
+}
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java
new file mode 100644
index 0000000000..dea6540973
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java
@@ -0,0 +1,56 @@
+package com.baeldung.mongodb.services;
+
+import java.io.IOException;
+
+import org.bson.types.ObjectId;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.data.mongodb.gridfs.GridFsOperations;
+import org.springframework.data.mongodb.gridfs.GridFsTemplate;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.baeldung.mongodb.models.Video;
+import com.mongodb.BasicDBObject;
+import com.mongodb.DBObject;
+import com.mongodb.client.gridfs.model.GridFSFile;
+
+@Service
+public class VideoService {
+
+ @Autowired
+ private GridFsTemplate gridFsTemplate;
+
+ @Autowired
+ private GridFsOperations operations;
+
+ public Video getVideo(String id) {
+ Video video = null;
+ GridFSFile file = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
+ if (file != null) {
+ video = new Video();
+ video.setTitle(file.getMetadata().get("title").toString());
+ try {
+ video.setStream(operations.getResource(file).getInputStream());
+ } catch (IOException e) {
+ return null;
+ }
+ }
+ return video;
+ }
+
+ public String addVideo(String title, MultipartFile file) {
+ DBObject metaData = new BasicDBObject();
+ metaData.put("type", "video");
+ metaData.put("title", title);
+ ObjectId id;
+ try {
+ id = gridFsTemplate.store(file.getInputStream(), file.getName(), file.getContentType(), metaData);
+ } catch (IOException e) {
+ return null;
+ }
+ return id.toString();
+ }
+
+}
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java
new file mode 100644
index 0000000000..fffb6ec320
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java
@@ -0,0 +1,50 @@
+package com.baeldung.mongodb.web;
+
+import java.util.Base64;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.baeldung.mongodb.models.Photo;
+import com.baeldung.mongodb.services.PhotoService;
+
+@Controller
+public class PhotoController {
+
+ @Autowired
+ private PhotoService photoService;
+
+ @GetMapping("/photos/{id}")
+ public String getPhoto(@PathVariable String id, Model model) {
+ Photo photo = photoService.getPhoto(id);
+ if (photo != null) {
+ model.addAttribute("title", photo.getTitle());
+ model.addAttribute("image", Base64.getEncoder().encodeToString(photo.getImage().getData()));
+ return "photos";
+ }
+ model.addAttribute("message", "Photo not found");
+ return "index";
+ }
+
+ @GetMapping("/photos/upload")
+ public String uploadPhoto(Model model) {
+ model.addAttribute("message", "hello");
+ return "uploadPhoto";
+ }
+
+ @PostMapping("/photos/add")
+ public String addPhoto(@RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model) {
+ String id = photoService.addPhoto(title, image);
+ if (id == null) {
+ model.addAttribute("message", "Error Occurred");
+ return "index";
+ }
+ return "redirect:/photos/" + id;
+ }
+}
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java
new file mode 100644
index 0000000000..e5fda0cc64
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java
@@ -0,0 +1,67 @@
+package com.baeldung.mongodb.web;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.util.FileCopyUtils;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.multipart.MultipartFile;
+
+import com.baeldung.mongodb.models.Video;
+import com.baeldung.mongodb.services.VideoService;
+
+@Controller
+public class VideoController {
+
+ @Autowired
+ private VideoService videoService;
+
+ @GetMapping("/videos/{id}")
+ public String getVideo(@PathVariable String id, Model model) {
+ Video video = videoService.getVideo(id);
+ if (video != null) {
+ model.addAttribute("title", video.getTitle());
+ model.addAttribute("url", "/videos/stream/" + id);
+ return "videos";
+ }
+ model.addAttribute("message", "Video not found");
+ return "index";
+ }
+
+ @GetMapping("/videos/stream/{id}")
+ public void streamVideo(@PathVariable String id, HttpServletResponse response) {
+ Video video = videoService.getVideo(id);
+ if (video != null) {
+ try {
+ FileCopyUtils.copy(video.getStream(), response.getOutputStream());
+ } catch (IOException e) {
+ response.setStatus(500);
+ }
+ } else {
+ response.setStatus(404);
+ }
+ }
+
+ @GetMapping("/videos/upload")
+ public String uploadVideo(Model model) {
+ model.addAttribute("message", "hello");
+ return "uploadVideo";
+ }
+
+ @PostMapping("/videos/add")
+ public String addVideo(@RequestParam("title") String title, @RequestParam("file") MultipartFile file, Model model) {
+ String id = videoService.addVideo(title, file);
+ if (id == null) {
+ model.addAttribute("message", "Error Occurred");
+ return "index";
+ }
+ return "redirect:/videos/" + id;
+ }
+}
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties
index 5b1b8000d0..6548f2b28a 100644
--- a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties
@@ -1,8 +1,13 @@
spring.application.name=spring-boot-persistence
-server.port=${PORT:0}
+server.port=8082
#spring boot mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=springboot-mongo
+spring.thymeleaf.cache=false
+
+spring.servlet.multipart.max-file-size=256MB
+spring.servlet.multipart.max-request-size=256MB
+spring.servlet.multipart.enabled=true
\ No newline at end of file
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/index.html b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/index.html
new file mode 100644
index 0000000000..f99e9caa8b
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/index.html
@@ -0,0 +1,14 @@
+
+
+
+Upload Files MongoDB
+
+
+ Home Page
+ Message
+
+ Upload new Photo
+
+ Upload new Video
+
+
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/photos.html b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/photos.html
new file mode 100644
index 0000000000..b5d56d020a
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/photos.html
@@ -0,0 +1,10 @@
+
+
+ View Photo
+ Title: name
+
+
+
+ Back to home page
+
+
\ No newline at end of file
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/uploadPhoto.html b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/uploadPhoto.html
new file mode 100644
index 0000000000..6d51c06d8f
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/uploadPhoto.html
@@ -0,0 +1,15 @@
+
+
+
+Upload new Photo
+
+
+
+
\ No newline at end of file
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/uploadVideo.html b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/uploadVideo.html
new file mode 100644
index 0000000000..ebc29d98e2
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/uploadVideo.html
@@ -0,0 +1,15 @@
+
+
+
+Upload new Video
+
+
+
+
\ No newline at end of file
diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/videos.html b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/videos.html
new file mode 100644
index 0000000000..b0ee0fc577
--- /dev/null
+++ b/persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/videos.html
@@ -0,0 +1,15 @@
+
+
+ View Video
+ Title: title
+
+
+
+
+
+
+
+
+ Back to home page
+
+
\ No newline at end of file