【问题标题】:By using spring framework how to design a web page which shows videos to the user通过使用spring框架如何设计一个向用户显示视频的网页
【发布时间】:2023-03-23 17:10:01
【问题描述】:

我们正在开发一个 Web 应用程序,我们必须在其中向用户显示视频列表,因此面临的 chrome 问题不允许视频转发,因此请建议我们提供更好的解决方案

控制器代码

@RequestMapping(value="GetVideo.ht", method=RequestMethod.GET)
public void getImage(@RequestParam("video_name") String name,
    HttpServletResponse response,
    HttpServletRequest request) throws IOException {
    System.out.println("I am from GetVideo.ht");

    HttpHeaders headers = new HttpHeaders();
    // response.setContentType("video/mp4");
    headers.set("Accept-Ranges", "bytes");
    headers.set("Content-Range", "bytes=0-1025/9044858");
    headers.set("Content-Transfer-Encoding", "binary");
    headers.set("Content-Length", "9044858");

    String decrypt = Enc.DecryptText(name);
    System.out.println("decry=" + decrypt);
    File imageFile = new File(decrypt);
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
    response.setHeader("Content-Disposition", "attachment; filename=" + imageFile.getName().replace(" ", "_"));

    InputStream in = new FileInputStream(imageFile);
    IOUtils.copy(in, response.getOutputStream()); 
    in.close();
}

在jsp页面中

<c:forEach items="${user_videos}" var="v" varStatus="status">
  <div id="${status.index}" class="tabcontent">
    <div class="col-lg-12 text-center pt-4">
      <video class="video-js vjs-default-skin" width="670" height="400" 
        controls id="vid${v.video_id}" onplay="loadVideo(${v.video_id},${status.index})">
        <source src="GetVideo.ht?video_name=${v.uploaded_video}" 
                type="video/mp4" >
      </video>
</c:forEach>

【问题讨论】:

    标签: java html spring jsp


    【解决方案1】:

    您的控制器需要支持字节范围请求。但是为什么不将Spring Content 用于解决方案的视频内容部分呢?这样您就不需要实现上面介绍的任何视频内容处理。 Spring Content 将为您提供这个。

    将 Spring 内容添加到您的项目中:

    pom.xml

    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>spring-content-rest-boot-starter</artifactId>
        <version>0.10.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.paulcwarren</groupId>
        <artifactId>content-fs-spring-boot-starter</artifactId>
        <version>0.10.0</version>
    </dependency>
    

    将内容与视频实体相关联(例如)。

    视频.java

    @Entity
    public class Video {
       ...
    
       @ContentId
       private String contentId;
    
       @ContentLength
       private Long contentLength;
    
       @MimeType
       private String mimeType;
    
       ...
    

    VideoLibrary.java

    创建一个VideoLibrary 接口。

      @StoreRestResource(path="videos")
      public interface VideoLibrary extends ContentStore<Video,String> {
        //
      }
    

    通过@EnableFilesystemStores 将它们捆绑在一起,并为您的视频库提供一个“永久文件夹”。这是上传的视频将上传到和流式传输的地方。

    SpringBootApplication.java

    @SpringBootApplication
    public class YourSpringBootApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(YourSpringBootApplication.class, args);
      }
    
      @Configuration
      @EnableFilesystemStores
      public static class StoreConfig {
        File videoLibrary() {
            return new File("/path/to/your/videos");
        }
    
        @Bean
        public FileSystemResourceLoader fsResourceLoader() throws Exception {
          return new FileSystemResourceLoader(videoLibary().getAbsolutePath());
        }
      }
    
    }
    

    这就是在/videos 创建基于 REST 的视频库服务所需的全部内容。本质上,当您的应用程序启动时,Spring Content 将查看您的依赖项(查看 Spring Content FS/REST),查看您的 VideoLibrary 接口并在文件系统上注入该接口的实现。它还将注入一个Controller,该Controller 也将http 请求转发到该实现。这使您不必自己实现任何这些。

    所以...

    POST /videos/{video-entity-id}

    使用 multipart/form-data 请求会将视频存储在 /path/to/your/videos 中,并将其与 id 为 video-entity-id 的视频实体相关联。

    GET /videos/{video-entity-id}

    将再次获取它。这支持部分字节范围请求;即您需要的视频流。

    等等...支持完整的 CRUD。

    您只需要更新您的 HTML 以调用正确的 URI:

    <c:forEach items="${user_videos}" var="v" varStatus="status">
      <div id="${status.index}" class="tabcontent">
        <div class="col-lg-12 text-center pt-4">
          <video class="video-js vjs-default-skin" width="670" height="400" 
            controls id="vid${v.video_id}">
            <source src="/videos/${v.uploaded_video}" type="video/mp4" >
          </video>
    </c:forEach>
    
    

    有一些入门指南here。参考指南是here。还有一个教程视频here。编码位从大约 1/2 处开始。

    HTH

    【讨论】:

      猜你喜欢
      • 2021-08-05
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-31
      • 1970-01-01
      • 2011-10-10
      相关资源
      最近更新 更多