前端 html body 中加入如下
<video id="video" width="600" height="360" preload controls>
<source src="http://xxxxx/getFile?fileNameId=aaa&type=bbbb" type='video/mp4'>
</video>
后端 controller 代码
@RequestMapping(value = "/getFile")
@ResponseBody
public void getFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileNameId") String fileNameId, @RequestParam("type") String type){
InputStream is = null;
ByteArrayOutputStream baos = null;
OutputStream os = null;
try {
is = fileSvc.getFileById(fileNameId);
baos = new ByteArrayOutputStream();
os = response.getOutputStream();
int ch;
while (-1 != (ch = is.read())) {
baos.write(ch);
}
byte[] data = baos.toByteArray();//Content-Type: video/mp4
if(type.equals("mp4")){
response.addHeader("Content-Type","video/mp4");
String rangeStr = request.getHeader("Range");
if(StringUtils.isEmpty(rangeStr)){
os.write(data);
}else {
response.setHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Content-Length","" + data.length);
response.addHeader("accept-ranges","bytes");
response.addHeader("Content-Range","bytes 0-" + (data.length-1) + "/"+ (data.length));
os.write(data);
}
}else {
os.write(data);
}
} catch (IOException e) {
if(e instanceof ClientAbortException){
}else {
e.printStackTrace();
}
}finally {
try {
if(is!=null)is.close();
if(baos!=null)baos.close();
if(os!=null)os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
还总结啥呢,直接贴代码不香嘛,拿走