思路:

上传:<form>表单提交视频-->后台使用字节流保存到本地。
展示:<video>标签展示: src属性发送请求 --> 使用字节流将视频绑定到响应并返回。

这条思路适用于所有文件(包括图片,音频,视频,压缩包),下面只是视频的实例。

一上传

1.form表单提交视频

<form method="post" action="/manager/card/addMovie" enctype="multipart/form-data">
<input name="movie" type="file" MULTIPLE>
<input type="submit">
</form> 注意<input>使用 type="file" MULTIPLE 属性
<form>使用 enctype="multipart/form-data"

2.controller接收

@RequestMapping("/addMovie")
public String addMovie(MultipartFile movie){
..................;
}

3.使用字节流保存到本地

/**
*
* @param file
* @param path 保存的路径
* @param fileName 保存的文件名
*/
public static void saveFile(MultipartFile file, String path, String fileName) { InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = file.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
try {
byte[] bs = new byte[1024]; // 读取到的数据长度
int len; // 输出的文件流保存到本地文件
File tempFile = new File(path); // 保存到临时文件 1K的数据缓冲
if (!tempFile.exists()) {
tempFile.mkdirs();
}
outputStream = new FileOutputStream(tempFile.getPath() + File.separator + fileName); while ((len = inputStream.read(bs)) != -1) { // 开始读取
outputStream.write(bs, 0, len);
} } catch (Exception e) {
e.printStackTrace();
} finally { // 完毕,关闭所有链接
try {
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

以上步骤视频就通过程序保存到电脑的指定位置了,一般我会新建一个视频类,先用uuid给视频重命名,视频类的路径是视频的名字,取的时候使用视频的名字去请求。

二 展示

1.video请求

    <video src="${file}/mp4+${mp4.paths}/${mp4.suffix}" controls="controls"
preload="auto">
</video> 注意:video要加controls="controls"才会有播放按钮显示,其他属性不一一介绍

2.使用字节流将视频绑定到响应并返回

@Controller
@RequestMapping("/file")
public class FileController {
/**
*
* @param response
* @param filePath 文件路径+名称
* @param suffix 文件的后缀
* @return
*/
@RequestMapping("/{filePath}/{suffix}")
public String getFile(HttpServletResponse response, @PathVariable String filePath, @PathVariable String suffix) {
FileInputStream fis = null;
ServletOutputStream outputStream = null;
int len = 0;
try {
File file = new File(FileUtils.getFileMainPath() + filePath + "." + suffix);
fis = new FileInputStream(file);
byte[] b = new byte[1024 * 2];
outputStream = response.getOutputStream();
while ((len = fis.read(b)) != -1) {
outputStream.write(b, 0, len);
}
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
if (outputStream != null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
fis = null;
outputStream = null;
}
}
return null;
}

}
等响应返回成功后video标签就显示了视频,

效果如下(我做的手机端的,所以比较小)

最新文章

  1. IDEA中maven搭建Spring+SpringMVC+mybatis项目
  2. c++错误——intermediate.manifest : general error c1010070很傻的错
  3. 免费开发者证书真机调试App ID失效问题:&quot;Unable to add App ID because the &#39;10&#39; App ID limit in &#39;7&#39; days has been exceeded.&quot;解决方案(5月5号)
  4. ubuntu安装skype
  5. 能用Shell就别编程-海量文本型数据的处理
  6. div左右布局
  7. 使用spring+mybatis+atomikos+tomcat构建分布式事务
  8. Spark SQL 1.3测试
  9. datable中table.row() not a funtion 解决方法
  10. oracle expdp导出时报 ora-39070:无法打开日志文件
  11. 初学CSS-3-文字的属性
  12. windows下解压zip包,包含中文解析
  13. Android简易项目--傻瓜式阿拉伯语输入法(Dummy Arabic Input)
  14. Java的Annotation标签
  15. yii2自定义json格式success,error跳转
  16. sqlserver2008事物处理---待续
  17. HTML5 -- 浏览器数据缓存 -- indexedDB
  18. Java并发(十五):并发工具类——信号量Semaphore
  19. Android Fragment实现微信底部导航
  20. dyci——IOS动态代码注入

热门文章

  1. Linux网络命名空间
  2. How to use Remote-SSH in Windows
  3. Scala语法2
  4. Map 集合
  5. tensorflow源码解析之common_runtime-direct_session
  6. LGP5204题解
  7. ES77
  8. Ubuntu20安装nodejs和npm并切换阿里源
  9. 【网鼎杯2020青龙组】Web WriteUp
  10. 选择Key-Value Store