0

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.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Coldoe
  • 21
  • 1
  • 8
  • I never done anything like this, but my guess would be that the the `src` attribute of `video` expects a URL. So you can try to add an extra controller method that returns a ResponseEntity that contains an InputStreamResource (See https://stackoverflow.com/a/20335000/40064) to return the video content. – Wim Deblauwe Jan 04 '21 at 08:03

0 Answers0