Video Entity
public class Video {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
@NotNull(message = "Can't be null")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate dateOfVideo;
@Size(min = 3,message = "Title should be atleast 3 characters long")
@NotNull(message = "Can't be null")
private String title;
@Size(min = 1,max = 1024, message = "Description should be atleast 1 and max 1024 char long")
private String description;
private Long size;
@Lob
private byte[] content;
@Nullable
@Lob
private byte[] picture;
}
Controller to upload and display Video
@Controller
@RequestMapping("/video/")
public class videoController {
@Autowired
private videoService videoService;
@GetMapping("/addVideo")
public String getAddVideo(Model model)
{
model.addAttribute("videoToAdd", new Video ());
return "Video/videoUpload";
}
@PostMapping("/addVideo")
public String postAddVideo(@Valid @ModelAttribute("videoToAdd")Video video,
BindingResult result)
{
if(result.hasErrors())
{
return "Video/videoUpload";
}
this.videoService.addOrUpdateVideo(video);
return "redirect:/video/addVideo";
}
@GetMapping("/{id}")
public String getSingleVideo(@PathVariable Long id,Model model)
{
Optional<Video> videoOptional = this.videoService.findById(id);
if(videoOptional.isPresent())
{
model.addAttribute("videoToShow",videoOptional.get());
return "Video/getVideo";
}
return "redirect:/video/addVideo";
}
}
View to display Video
<!DOCTYPE html>
<html xmlns:th="http://thymeleaf.org"
lang="en">
<head>
<meta charset="UTF-8">
<title>Get single video</title>
<div th:replace="Shared/sharedBootStrap"></div>
</head>
<body>
<div class="container">
<h1 th:text="${videoToShow.Id}"></h1>
<video width="320" height="240" controls>
<source src="${videoToShow.content}" type="video/mp4">
</video>
</div>
</body>
</html>
Error from chrome => Failed to load resource: the server responded with a status of 400 ()
Should I convert byte to file/multipartfile or something like that before setting source for video?
Should I build a method like ResponseEntity?
I'm stuck with this problem. I've read about blob but most of tutorials are related to .jpg or .pdf files. I appreciate all help and tips.