Skip to content

Commit

Permalink
upload files mongodb and spring boot
Browse files Browse the repository at this point in the history
  • Loading branch information
Doha2012 committed Jun 22, 2019
1 parent 7d3639c commit 29912c9
Show file tree
Hide file tree
Showing 14 changed files with 389 additions and 1 deletion.
4 changes: 4 additions & 0 deletions persistence-modules/spring-boot-persistence-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- MongoDB -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Photo, String> {

}
Original file line number Diff line number Diff line change
@@ -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 + "]";
}

}
Original file line number Diff line number Diff line change
@@ -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 + "]";
}

}
Original file line number Diff line number Diff line change
@@ -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<Photo> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html xmlns:th="https://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload Files MongoDB</title>
</head>
<body>
<h1>Home Page</h1>
<div th:if="${message != null}" th:text="${message}">Message</div>
<br/>
<a href="/photos/upload">Upload new Photo</a>
<br/><br/>
<a href="/videos/upload">Upload new Video</a>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html xmlns:th="https://www.thymeleaf.org">
<body>
<h1>View Photo</h1>
Title: <span th:text="${title}">name</span>
<br/>
<img alt="sample" th:src="*{'data:image/png;base64,'+image}" width="200"/>
<br/> <br/>
<a href="/">Back to home page</a>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html xmlns:th="https://www.thymeleaf.org">
<body>

<h1>Upload new Photo</h1>
<form method="POST" action="/photos/add" enctype="multipart/form-data">
Title:<input type="text" name="title" />
<br/>
Image:<input type="file" name="image" accept="image/*" />
<br/>
<br/>
<input type="submit" value="Upload" />
</form>

</body>
</html>
Loading

0 comments on commit 29912c9

Please sign in to comment.