forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload files mongodb and spring boot
- Loading branch information
Showing
14 changed files
with
389 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...ing-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/daos/PhotoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Photo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ules/spring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/models/Video.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...ng-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/PhotoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ng-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/services/VideoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...ring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/PhotoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ring-boot-persistence-mongodb/src/main/java/com/baeldung/mongodb/web/VideoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
...istence-modules/spring-boot-persistence-mongodb/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
14 changes: 14 additions & 0 deletions
14
persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
10 changes: 10 additions & 0 deletions
10
persistence-modules/spring-boot-persistence-mongodb/src/main/resources/templates/photos.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
15 changes: 15 additions & 0 deletions
15
...nce-modules/spring-boot-persistence-mongodb/src/main/resources/templates/uploadPhoto.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.