正在读取blob文件(.mp4)并使用spring引导和thymeleaf为用户显示

zzzyeukh  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(287)

视频实体

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
@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";
    }
}

查看以显示视频

<!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>

chrome=>加载资源失败:服务器响应状态为400()
在设置视频源之前,我应该将字节转换成文件/多部分文件或类似的东西吗?
我应该建立一个像responseentity这样的方法吗?
我被这个问题困住了。我读过blob,但大多数教程都与.jpg或.pdf文件相关。我感谢所有的帮助和提示。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题