SpringBoot视频播放(前后端分离,Mac下开发)
SpringBoot视频播放(Mac下开发)一、准备mp4视频对格式有要求,视频编码要设置为 AVC(H264) ,音视频编码要设置为 AAC。详情见参考一。视频静态资源存放在resources/static/1.mp4。此外还需在pom.xml配置,以便编译时将静态资源加入,如下<!--用于保证将src/main/java下的xml等文件,能自动复制到classes文件夹中-->&l
·
SpringBoot视频播放(Mac下开发)
一、准备
mp4视频对格式有要求,视频编码要设置为 AVC(H264) ,音视频编码要设置为 AAC。详情见参考一。
视频静态资源存放在resources/static/1.mp4。此外还需在pom.xml配置,以便编译时将静态资源加入,如下
<!--用于保证将src/main/java下的xml等文件,能自动复制到classes文件夹中-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<!--<include>**/*.*</include>-->
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<!--<include>**/*.*</include>-->
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
<include>**/*.xlsx</include>
<include>**/*.mp4</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
**注意:**resources标签存在于project中的build标签下
二、代码
**注意:**具体代码参考于参考二,但由于我的硬件环境原因,参考二的代码并不能在我的电脑上正常运行,遂作出一些修改
新建类用于返回视频流
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import javax.servlet.http.HttpServletRequest;
import java.nio.file.Path;
@Component
public class NonStaticResourceHttpRequestHandler extends ResourceHttpRequestHandler {
public final static String ATTR_FILE = "NON-STATIC-FILE";
@Override
protected Resource getResource(HttpServletRequest request) {
final Path filePath = (Path) request.getAttribute(ATTR_FILE);
return new FileSystemResource(filePath);
}
}
Controller实现
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@RestController
@RequestMapping("/file")
@AllArgsConstructor
public class FileRestController {
private final NonStaticResourceHttpRequestHandler nonStaticResourceHttpRequestHandler;
@GetMapping("video")
public void videoPreview(HttpServletRequest request, HttpServletResponse response) throws Exception {
String sourcePath = ClassUtils.getDefaultClassLoader().getResource("").getPath().substring(1);
// 在mac环境下前面不加“/”读不到文件
String realPath = "/" + sourcePath + "static/1.mp4";
File file = new File(realPath);
Path filePath = Paths.get(realPath);
if (Files.exists(filePath)) {
// 这里也对参考代码作出修改,原先的Files.probeContentType(filePath)返回为null
MimetypesFileTypeMap m = new MimetypesFileTypeMap(filePath.toString());
String mimeType = m.getContentType(file);
if (!StringUtils.isEmpty(mimeType)) {
response.setContentType(mimeType);
}
request.setAttribute(NonStaticResourceHttpRequestHandler.ATTR_FILE, filePath);
nonStaticResourceHttpRequestHandler.handleRequest(request, response);
} else {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
}
}
}
三、测试
输入http://localhost:8080/file/video即可得到视频
参考
-
https://blog.csdn.net/Dear_xaiobo/article/details/95186205
-
https://blog.csdn.net/weixin_38759449/article/details/104496464
更多推荐
所有评论(0)