借助spring的resourcehttprequesthandler可以实现媒体数据的传输,比如在线播放视频、预览图片等。
目前已知spring boot传输视频流的方法
- 读取整个视频文件,然后把文件流写入httpservletresponse的outputstream。
(此方法可行,但是需要消耗较多的服务器资源,且客户端需要下载整个视频才能播放) - 使用http的range实现分片加载,但是需要手动实现,比较麻烦。
- 使用spring自带的resourcehttprequesthandler是最佳实践。
思路
resourcehttprequesthandler是spring boot用于加载静态资源的一个类,默认用于从"classpath:/static"等目录读取静态资源,以便前端访问。我们可以继承它自定义一个实现。
使用方法
在项目的config包(推荐)继承resourcehttprequesthandler并重写getresource方法,使其返回所需要呈现给前端的资源(org.springframework.core.io.resource)
package com.example.server.config; import jakarta.servlet.http.httpservletrequest; import org.springframework.core.io.resource; import org.springframework.core.io.urlresource; import org.springframework.lang.nonnull; import org.springframework.stereotype.component; import org.springframework.web.servlet.resource.resourcehttprequesthandler; import java.net.malformedurlexception; import java.nio.file.path; import java.util.list; @component public class customresourcehttprequesthandler extends resourcehttprequesthandler { private resource resource; @override protected resource getresource(@nonnull httpservletrequest request) { return this.resource; } public void setresource(path filepath) throws malformedurlexception { this.resource = new urlresource(filepath.touri()); setlocations(list.of(this.resource)); } }
控制层注入customresourcehttprequesthandler,并向setresource方法传入文件路径(java.nio.file.path),设置请求头,最后让customresourcehttprequesthandler处理请求。
import org.springframework.beans.factory.annotation.autowired; import org.springframework.http.httpheaders; import org.springframework.http.httpstatus; import org.springframework.http.responseentity; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.pathvariable; import org.springframework.web.bind.annotation.requestmapping; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.nio.file.path; import java.nio.file.paths; @controller @requestmapping("/files") public class filecontroller { @autowired private customresourcehttprequesthandler resourcehttprequesthandler; @getmapping("/{filename}") public responseentity<?> getfile(@pathvariable string filename, httpservletrequest request, httpservletresponse response) { try { // 解析文件路径 path filepath = paths.get("d:/storageservice").resolve(filename).normalize(); // 检查文件是否存在 if (!filepath.tofile().exists()) { return new responseentity<>(httpstatus.not_found); } // 设置资源路径 resourcehttprequesthandler.setresource(filepath); // 设置响应头,inline 会在浏览器中显示或播放文件 response.setheader(httpheaders.content_disposition, "inline; filename=\"" + filename + "\""); // 让 customresourcehttprequesthandler 处理请求 resourcehttprequesthandler.handlerequest(request, response); return new responseentity<>(httpstatus.ok); } catch (exception e) { return new responseentity<>(httpstatus.internal_server_error); } } }
注意点
控制层返回值需要responseentity类型(org.springframework.http.responseentity),且try-catch不能省略,否则可能会不断抛出异常(asyncrequestnotusableexception和ioexception)但不影响正常使用。
其他方案
如果有条件也可以使用minio,或者视频云点播vod。他们提供了现成的解决方案,通过调用api可以获取视频等文件的直链。
参考资料
springboot+vue播放视频流(无需下载视频,可以拖动进度、倍速播放)
到此这篇关于java的文章就介绍到这了,更多相关java内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论